PHP OOP Terms Explained - Part 2
Below I tried to explain some other PHP Object Oriented Programming terms. Feel free to ask questions.
Here is the first part : OOP Terms - Part 1
- We can create multiple object from the same class. Like,
a. $rahim = new Person();
b. $karim = new Person();
i.
$rakim->set_name(‘Nas’);
ii.
$karim->set_name(‘Eminem’);
Photo Credit: memegenerator.net
- After that we can access an object’s data.
echo “My name is: ”. $rakim->get_name();
- Constructors are one kind of special methods.
- 'construct' method starts with two underscores (__) and the word 'construct'.
- Remember, in a class
a. Functions = methods
b. Variables = properties
- Constructor is a special kind of method which must be executed.
- We can use access modifiers to control the access to properties and methods.
- If we do not defined as public, protected or private, it will be assumed as public.
- If the property is ‘private’, only that containing class can access the property.
- If the property is ‘protected’, only that class and extending child class can access that property.
- If the property is ‘public’, it can be accessed from anywhere.
Inheritance:
Inheritance helps us to reuse our
code. By using inheritance, we can use a base class by extending it into the
child class.
Such as:
class employee extends
person {
function function_name() {
//function_definition;
}
}
3 Here employee is the child class and person is
the parent or base class.
1 Method overriding:
When we want to change the activities
or behavior of a base class method in child class, we can re-declare it in our
child class using our own necessity. It is called method overriding.
No comments