Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP OOP Tutorial from beginner to advance - (Encapsulation - Part 9)


  • Please log in to reply
1 reply to this topic

#1 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 13 November 2012 - 05:05 AM

Hello everyone and welcome again into my tutorial, we are now at the part 9 of the PHP Object Oriented Basic Tutorials and we are going to tackle today is about Encapsulation. What Encapsulation is? When to use them and why?


What is Encapsulation?
Encapsulation is just when you want to hide stuffs into your object from the public. It's the idea of hiding information. For example you have an Animal Class, and it has a function called getName() then you've created a subclass of Animal called Cow. Now, when we used the function getName() it will return the name of the animal which is Cow. Now here's the problem What if someone created an Animal Object? Would you allow? Since it's a base class that some classes will inherit it's generic functions.. you wouldn't want someone to create an object from this class right?

Here comes Encapsulation to solve our problem :)

Please if you haven't read the lesson about Inheritance.. please go here and read it Inheritance and __construct() part5



We have three keywords that are involved in Encapsulation, namely

Protected - the method/function/property is available to the parent class and all inheritting class or we call then subclasses or child classes

Private - the method is private and only available to the parent class/base class.
Public - The method is publicly available and can be accessed by all subclasses.

Those three words are not so much difficult to understand correct? It's something like.. for example you are a parent and you want to Protect your children, so it means your children can only be accessed by your family members... >_< makes sense?

If you have something like a secret and you don't want anyone to let it know.. for sure you will keep it Private.

If you have a paper and when exam or quiz time your teacher ask everyone in the class to bring out one whole piece or paper, your classmates will go into you and you will share your paper Publicly (lol).

Now let's take a look on how to use those three keywords.

create 3 files
untitled.JPG

index.php
animal.php
cow.php

here's the content of animal.php



<?php
    class Animal {
     public function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>


in this animal class we have 1 public function called getName() with an argument or parameter.
it's public so? You're right everything can accessed that function as long as I included that php file.


for the cow.php



<?php
class Cow extends Animal{
public function sayName() {
$this->getName(get_class($this));
}
}
?>

in this I created an Animal class and we used the function get_class() to get the name of the class
notice that I've also it extends to the Animal class.. it means that this class is a subclass or Animal class so everything in the Animal class is shared in this class unless they're not private.

here's the content of index.php

<?php
include_once('animal.php');
include_once('cow.php');

$cow = new Cow();
$cow->sayName();
?>

in this file, I've included animal.php and cow.php so we can use them both in this php file,

first I've created an object

$cow = new Cow();

then I called the sayName(); in the $cow object.. and the sayName() function calls the getName() function, we all know that getName() function is a function from the parent class of Cow which is Animal
so the output would be


This is the Cow Class


so what happens if we created an Animal object?


$animal = new Animal();
$animal->getName("blah");

output :

This is the blah Class



oooppsss that's bad since I only want that getName() function to be accessed from all the subclasses of Animal only.. so I have to change it from Public to Protected


<?php
    class Animal {
     protected function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>

now what will happened if we created an Animal object like we did before?

output :

Fatal error: Call to protected method Animal::getName()

uh oh.. we got an error because we can't just accessed that because it's protected and only it's base class and subclasses can access that method.

what if we made it private and we call that function inside our cow.php?

animal.php

<?php
    class Animal {
     private function getName($arg) {
     echo "This is the ".$arg." Class";
     }
    }
?>

cow.php


<?php
class Cow extends Animal{
public function sayName() {
$this->getName(get_class($this));
}
}
?>

index.php


<?php
include_once('animal.php');
include_once('cow.php');

$cow = new Cow();
$cow->sayName();
?>


here's the output if you tried to run it

Call to private method Animal::getName()

ofcourse.. you can't call that method because it's private and can only be accessed by it's base class.


Conclusion
Today we've learned the use of encapsulation, this technique in programming is really useful if we are creating a function or some variables that we want to hide from some users.. It's good if you are creating a framework, you wouldn't want some programmers messed up your code and use it the wrong way. Thank you so much for reading my tutorial.
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 sgaldorJasonStorm

sgaldorJasonStorm

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 07 December 2012 - 11:47 AM

Where are the other 8 parts of this tut?
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download