You will be able to use or implement either one of the common design patterns. My favorite is the observer pattern and Singleton pattern.
Benefits in writing codes in OOP
1. One or more classes can be derived from a base class. This is called inheritance. As the application grows overtime, new methods are needed to accomodate the growth .
For example, if we have a bicycle class responsible for displaying information all about bicycle, years later our applications needs to provide information for both the bicycle and the motorcycle. For non-OOP programmers, the first thing that will come to mind to accomodate this change is to trash bin the original bycycle class and create a new one that will handle both. However, for the OOP programmers, the first thing they do is to analyze what makes motorcycle similar to bicycle. All features of motorcyle that are not in the bicycle will be aded in the extended bicycle class.
A simple example to clarify my statement 1 above.
class Bicycle{
public $wheels, $chain, $power;
public function __construct($wheels, $chain, $power){
$this->wheels = $wheels;
$this->chain = $chain;
$this->power = $chain;
}
public function get_wheels(){
return $this->wheels;
}
public function get_chain(){
return $this->$chain;
}
public function get_power(){
return $this->power;
}
}
Example of inheritance, extending the bicycle class above to create a new child class motorcycle.
class Motorcycle extends Bicycle{
public $wide_wheels, $wide_chain, $motor_power;
public function __construct($wheels, $chain, $power, $wide_wheels, $wide_chain, $motor_power ){
parent::__construct($wheels, $chain, $power);
$this->widerWheels = $wide_wheels;
$this->widerChain = $wide_chain;
$this->motorPower = $motor_power;
}
public function get_WideWheels(){
return $this->widerWheels;
}
public function get_WideChains(){
return $this->widerChain;
}
public function get_MotorPower(){
return $this->motorPower;
}
We can then set instances of both the parent and the child class like this..
$ObjectBike = new Bicycle('flimsy', 'single', 'foot power');
$ObjectHarley = new Motorcycle('ultra wid', 'gear drive', 'motor power');
A well written Class can take advantage of templating engine. Meaning all strings are passed or assigned to the template engine, without calling NOT a single echo inside the class methods.
By using a singleton pattern, we can make sure there is ONLY ONE instance of that class.
Application written in OOP can be converted later into MVC framework.
Advantage of functions over the confined methods within class.
1. Application can be deployed a lot faster, but this can lead to spaghetti like codes over time.
2. Requires less file. Normally, OOP written codes requires lots of files, because of separation of many different classes doing different jobs within the application.
3. Does not require highly organized bootstrapping. Normally, OOP based application requires a file manager script to handle the application's required files. Howerver, this is one of the unique properties of a well organized application.