About Object Oriented Programming
Object oriented programming (OOP) was introduced in PHP 5. This new technique of programming allows us to define classes and create objects. The aim is to reduce rewriting of the same code again and again while increasing reusability of the available code. OOP also makes it simper to edit and update our code.
OOP looks a bit complex at first but once you begin to understand it, you will know how much efficient it is. It is not absolutely necessary to program the OOP way and you can still stick to the procedural method. However, it is considered as a good practice to write code in OOP and there are a lot of benefits. If you thought functions in PHP made it easier, think about how much better it will be to have a collection of functions inside a single class that is reusable. Besides, some languages like Java require you to write code the OOP way. So, you might as well get the hang of it.
In OOP, we define classes and use those classes to create objects. Think of classes as blueprints and objects as the actual buildings that we build. We can build as many buildings or houses we want using the blueprint. Classes in programming are no different. Define a class and use it to create as many objects as you want to.
Basic Terminology Used in Object Oriented Programming
There are a lot of new terminologies used in OOP, but let’s just get accustomed to the basic ones.
Class: A template that contains methods and properties for use in objects. This is the main blueprint.
Objects: When you create new instance of classes, it is known as an object. You can create many objects from a single class.
Methods: In simple words, they are functions that are inside a class.
Properties: Simply saying, they are variables that are present inside classes.
Public vs. Private vs. Protected
While declaring properties and methods inside classes, they can be of different types. They can be public, private or protected. This is known as their visibility.
Here is how they differ:
- Public: Can be accessed anywhere from your PHP code. That means, you can get their value or access the methods from other classes and object instances.
- Private: Can’t be accessed on outside of class. Their value or method can be used inside that class only. They are exclusive to a single class.
- Protected: Can only be accessed from within your class or the classes that are extended to inherit the parent class.
Creating a Class
Here’s an example of how you create a class
<?php class className{ //properties and methods } ?>
Here’s how you create a new object from the class
<?php $object = new className(); ?>
You can create multiple objects from the same class as:
<?php $object = new className(); $object2 = new className(); ?>
A class can have different properties.
Example:
<?php class className{ public $name; private $address="Nepal"; protected $phone = 42241; } ?>
It can also have methods.
Example:
<?php class className{ public $name; public function printName($name){ return $this->name; } } ?>
Accessing Properties and Methods
Objects are created out of classes. These objects utilize the properties and methods present inside those classes.
Here’s how you access properties inside classes
<?php class className{ public $name="Ashish"; public $address; } $info = new className(); $info->address = "Kathmandu"; echo $info->name; echo "<br />"; echo $info->address; ?>
Output: Ashish Kathmandu
Methods can be created inside classes and they can be used by objects.
Example:
<?php class className{ public $name; public function printName($name){ $this->name = $name; echo $this->name; } } $info = new className(); $info->printName("Ashish"); ?>
Output: Ashish
Creating Multiple Instances of Objects
Classes can have as many objects as you want. Just use a different name for them and you will create multiple instances of a single class. This is how code reusability works.
Example:
<?php class className{ public $name; public function printName($name){ $this->name = $name; return $this->name; } } $obj1 = new className(); $obj2 = new className(); echo $obj1->printName("Ashish"); echo "<br />"; echo $obj2->printName("Michael"); ?>
Output: Ashish Michael
__construct Methods
__construct is a special method used in classes. This constructor method is executed immediately when a class is called. It is like the default method. It can also take in parameters or arguments.
Example:
<?php class className{ public $name; public function __construct($name){ $this->name = $name; echo $this->name; } } $info = new className("Ashish"); ?>
Output: Ashish
Inheritance of classes using extends
One class can inherit properties and methods from another class. This is called inheritance. When one class inherits another, the new class will have access to all the public and protected methods and properties of the parent class. The new class can then have additional lines of code.
For example
<?php class person{ public $living = true; } class girl extends person{ } $zoe = new girl; echo $zoe->living; ?>
Output: 1
You can simply override the values of public and protected methods and properties in the parent class by defining the variable again. If the property in the parent method is a boolean, it seems to throw an error when trying to redeclare them.
Example:
<?php class person{ public $living = "true"; } class girl extends person{ public $living = "Not Sure"; } $zoe = new girl; echo $zoe->living; ?>
Output: Not Sure
Constants
If you want the value assigned to a property to not change, then we make use of constants. We have to define them and access them in a different way.
<?php class unchangeable{ const change = 5433; const unchange = "Ashish"; public function access(){ echo self::unchange; } } echo unchangeable::change; echo "<br />"; $object = new unchangeable; $object->access(); ?>
Output: 5433 Ashish
- We do not need to create a new object to access the constant change.
- Using $echo $object->change won’t work even when we create a new object.
- If we have to access the constant within the class itself we use self::constantname. Using $this-> won’t work.
Static Keyword
Static keywords is used to access properties and methods without creating a new instance of the class. We use the “::” operator just like we use for accessing constants.
Example:
<?php class Example{ public static function hello(){ echo "Hello my honey!"; } } Example::hello(); ?>
Output: Hello my honey!
Checking Existence
Simple but important built in methods to check whether objects instances, properties and methods exists or not.
is_a: Checks whether an object is an instance of a class.
property_exists: Checks whether a property exists or not within a new instance of a class.
method_exists: Checks whether or not a method exists within a new instance of a class.
Example:
<?php class className{ public $name; public function printName($name){ $this->name = $name; return $this->name; } } $object = new className(); if(is_a($object,"className")){ echo "Object belongs to className."; echo "<br />"; } if(property_exists($object, "name")){ echo "Property name exists."; echo "<br />"; } if(method_exists($object, "printName")){ echo "Method printName exists."; } ?>
Output: Object belongs to className. Property name exists. Method printName exists.
Naming standards
Here are are the popular naming standards for public, private and protected elements:
- public $yourName;
- private $_yourName;
- protected $_TyourName;