New tutorials View all
Encapsulation in PHP
Abstract classes in PHP
An introduction to MySQL databases in PHP
Creating a user login and registration system.
First Lesson of HTML
New forum replies
Confused in The big and general IRC Bot...Jim in Php irc bot logwescooldude3 in Php irc bot logJim in AJAX + loadingJim in Php irc bot say namewescooldude3 in Php irc bot say nameRealShadow in AJAX + loading
New frontpage replies
tutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHP
New articles
New news
Encapsulation in PHP (1231 views)
You might wonder, what the heck is Encapsulation? Well you must have seen classes with Private and Public stuff right? Must read for advanced PHP programmers.
For this tutorial I assume you have basic knowledge of Object Orientation since encapsulation is part of thinking OOP. If you don't, read this tutorial: Object Orientated programming in PHP: The absolute basics.
Also, please note that encapsulation is only possible since PHP5.
First some theory
You know that classes have properties (the variables) and methods (the functions). Normally everything in your code can just use all the properties, this can cause problems.
Imaging you are working on a big project with 10 people, you can never know the code of others into the detail. But from another hand, you must be able to use the functionality of other programmers. This is where OOP kicks in, you only have to learn what functions and properties you have to use.
When you would have an FTP class, you don't need to learn the FTP protocol, you just have to learn the functions to connect, to navigate, to download and to upload.
But this can give problems. How do you know which properties and methods you can use? You might be calling a method that is design to be called from the inside of the class. Of course this problem can be solved rather quickly with some error checking, but in the end that still doesn't solve the problem of it being difficult to use by another programmer.
That's where encapsulation is designed for. Encapsulation is limiting the access of properties and methods from the outside world. With this technique you can tell each property and method if it's save to be called from the outside or if it can only called from the inside.
With encapsulation there are several types, I will discuss three of them: public, protected and private.
Public
When making something public it is accessible by everything, you can just create an instance (object) of the class and call the function. This is basically just like if you would create a method without encapsulation, it's free as a bird. \o/
Protected
This time you cannot just inherit an object from the class and call the property or method. You will receive an warning, it just wont work.
However, you are able to use the property or method in a subclass. Imaging you have the class Connection which takes care of all kinds of connections. If you would create a subclass for that class named SQL you will be able to call the protected members from the Connection class in the SQL class.
So basically, nobody can access protected members except for subclasses.
Private
A private method or property is only accessible by it's own class. You cannot even access it from a subclass. Try it from the outside and you will get an error.
Encapsulation in PHP!
Let's test our freshly learned information and put it to the test. First create a new class:
This little code outputs:
Called successfully
Called successfully
First time we immediately create an object from the myParentClass class. Next we try to call the public method of the class, it works because it's public and like learned earlier all public members can be always accessed.
Second is creating an object from the myChildClass class. This time the function we call refers to the parent function. Again this works because public can always be accessed (kind of useless example right?)
Now lets try protected!
This results into:
Fatal error: Call to protected method myParentClass::myFunction() from context '' in /var/vhost/jim.zonax.net/index.php on line 21
This is because in we try to call the protected method immidiatly from the $parentObject object. This won't work because protected means that only subclasses can access it.
So what if we remove the call from the $parentObject object?
As you can see, I just removed the myParentClass object. In this example I only try to call the method from within a subclass of the parent class. This time the result is:
Called successfully
Of course because a protected method can be accessed by a subclass.
Ok now for the private…
We first try to call the private method immediately, this fails:
Fatal error: Call to private method myParentClass::myFunction() from context '' in /var/vhost/jim.zonax.net/index.php on line 21
Ok, now lets try to call it from the subclass ok?
Damn again an error:
Fatal error: Call to private method myParentClass::myfunction() from context 'myChildClass' in /var/vhost/jim.zonax.net/index.php on line 14
This time it can't be accessed by the subclass.
Lets try to call it from within the class itself:
Look carefully, first we are calling the public method from the subclass. Of course this works so the function tries to call it's parents method callMyFunction. Again this works because that function is just protected so the subclass can easily access it. This function tries to access the function myFunction and again this works because the private function is inside the same class! This will output:
Called successfully
In this last example we made a weird chain of function calls, but imaging you building the class now. Your co-workers can just check out the public methods to see which functions are save to call. Or the protected methods if they would like to extend that class.
So you have learned basic encapsulation, I recommend you trying some of it right now.
Also, please note that encapsulation is only possible since PHP5.
First some theory
You know that classes have properties (the variables) and methods (the functions). Normally everything in your code can just use all the properties, this can cause problems.
Imaging you are working on a big project with 10 people, you can never know the code of others into the detail. But from another hand, you must be able to use the functionality of other programmers. This is where OOP kicks in, you only have to learn what functions and properties you have to use.
When you would have an FTP class, you don't need to learn the FTP protocol, you just have to learn the functions to connect, to navigate, to download and to upload.
But this can give problems. How do you know which properties and methods you can use? You might be calling a method that is design to be called from the inside of the class. Of course this problem can be solved rather quickly with some error checking, but in the end that still doesn't solve the problem of it being difficult to use by another programmer.
That's where encapsulation is designed for. Encapsulation is limiting the access of properties and methods from the outside world. With this technique you can tell each property and method if it's save to be called from the outside or if it can only called from the inside.
With encapsulation there are several types, I will discuss three of them: public, protected and private.
Public
When making something public it is accessible by everything, you can just create an instance (object) of the class and call the function. This is basically just like if you would create a method without encapsulation, it's free as a bird. \o/
Protected
This time you cannot just inherit an object from the class and call the property or method. You will receive an warning, it just wont work.
However, you are able to use the property or method in a subclass. Imaging you have the class Connection which takes care of all kinds of connections. If you would create a subclass for that class named SQL you will be able to call the protected members from the Connection class in the SQL class.
So basically, nobody can access protected members except for subclasses.
Private
A private method or property is only accessible by it's own class. You cannot even access it from a subclass. Try it from the outside and you will get an error.
Encapsulation in PHP!
Let's test our freshly learned information and put it to the test. First create a new class:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php class myParentClass { public function myFunction() { echo 'Called successfully <br>'; } } class myChildClass extends myParentClass { public function myChildFunction() { parent::myFunction(); } } // Parent object $parentObject = new myParentClass(); $parentObject->myFunction(); // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
This little code outputs:
Called successfully
Called successfully
First time we immediately create an object from the myParentClass class. Next we try to call the public method of the class, it works because it's public and like learned earlier all public members can be always accessed.
Second is creating an object from the myChildClass class. This time the function we call refers to the parent function. Again this works because public can always be accessed (kind of useless example right?)
Now lets try protected!
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php class myParentClass { protected function myFunction() { echo 'Called successfully <br>'; } } class myChildClass extends myParentClass { public function myChildFunction() { parent::myFunction(); } } // Parent object $parentObject = new myParentClass(); $parentObject->myFunction(); // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
This results into:
Fatal error: Call to protected method myParentClass::myFunction() from context '' in /var/vhost/jim.zonax.net/index.php on line 21
This is because in we try to call the protected method immidiatly from the $parentObject object. This won't work because protected means that only subclasses can access it.
So what if we remove the call from the $parentObject object?
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php class myParentClass { protected function myFunction() { echo 'Called successfully <br>'; } } class myChildClass extends myParentClass { public function myChildFunction() { parent::myFunction(); } } // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
As you can see, I just removed the myParentClass object. In this example I only try to call the method from within a subclass of the parent class. This time the result is:
Called successfully
Of course because a protected method can be accessed by a subclass.
Ok now for the private…
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php class myParentClass { private function myFunction() { echo 'Called successfully <br>'; } } class myChildClass extends myParentClass { public function myChildFunction() { parent::myFunction(); } } // Parent object $parentObject = new myParentClass(); $parentObject->myFunction(); // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
We first try to call the private method immediately, this fails:
Fatal error: Call to private method myParentClass::myFunction() from context '' in /var/vhost/jim.zonax.net/index.php on line 21
Ok, now lets try to call it from the subclass ok?
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php class myParentClass { private function myFunction() { echo 'Called successfully <br>'; } } class myChildClass extends myParentClass { public function myChildFunction() { parent::myFunction(); } } // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
Damn again an error:
Fatal error: Call to private method myParentClass::myfunction() from context 'myChildClass' in /var/vhost/jim.zonax.net/index.php on line 14
This time it can't be accessed by the subclass.
Lets try to call it from within the class itself:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php class myParentClass { private function myFunction() { echo 'Called successfully <br>'; } protected function callMyFunction() { $this->myFunction(); } } class myChildClass extends myParentClass { public function myChildFunction() { parent::callMyFunction(); } } // Child object $childObject = new myChildClass(); $childObject->myChildFunction(); ?> |
Look carefully, first we are calling the public method from the subclass. Of course this works so the function tries to call it's parents method callMyFunction. Again this works because that function is just protected so the subclass can easily access it. This function tries to access the function myFunction and again this works because the private function is inside the same class! This will output:
Called successfully
In this last example we made a weird chain of function calls, but imaging you building the class now. Your co-workers can just check out the public methods to see which functions are save to call. Or the protected methods if they would like to extend that class.
So you have learned basic encapsulation, I recommend you trying some of it right now.

Replies on Encapsulation in PHP:
Jump to comment page: 1
By Jim on Monday 18 August 2008 12:15
Thanks mate, was it easy enough to understand? (Well not a difficult subject anyway)
Jump to comment page: 1
You are not logged in. Please login or register an account, it just takes 30 seconds.

Great tutorial Jim.