Learning OOP: Restricting access using access modifiers Part 2/227, July 2010 [
<< Part 1 of Learning OOP: Restricting access using modifiers |
# ]
This is the second part of the tutorial. In the previous tutorial, we discussed two new keywords and those keywords are what define access to either your methods or attributes. Just a recap,
public is the default without declaring it, and allows the object to have access to it.
private will not allow access, outside of the class. Now, there are 3 in total, so the next one is called
protected. This is a mix of private and public. However, I can only demonstrate it in a good form, by using two classes. So, this means another new keyword today will be
extends. Let's get started.
Parent and childOne of the best ways to describe a practice of extending a class is to create a class called Parent, and create a class called Child. Now, you should all know that the child will inherit the mother's looks and genetics. So in the OO world, the Child 'extends' the Parent. Let's look at this example:
Code:
<?php
class mParent {
protected static $firstname = 'Brown';
protected static $secondname = 'Cow';
public function myName() {
echo "My name is " . self::$firstname . " " . self::$secondname . ".";
return true;
}
}
class mChild extends mParent {
protected static $firstname = 'Yellow';
protected static $secondname;
public function myName() {
self::$secondname = parent::$secondname;
echo "<br/>My name is " . self::$firstname . " " . self::$secondname . ".";
return true;
}
}
$Parent = new mParent;
$Parent->myName();
$Child = new mChild;
$Child->myName();
?>
So, at first, it is still probably a little intimidating; but let's break it down. Firstly, just ignore the keyword
static, without it there you will not be able to access attributes. Now, the mParent class has two attributes, $firstname, and $secondname. Both self-explanatory. So, the first name of the parent, mParent is "Brown". The second name/or last name is "Cow". The child of that parent needs to have access to one of these attributes, so therefore, needs to extend their parent, which is mParent.
Calling mParent's method myName() will print "My name is Brown Cow." mChild has two attributes, both the same. $firstname is set, however, $secondname is not. In mChild's method logic of myName(), I assign mChild's second name by refering to itself using the
self keyword to mParent's second name. The keyword
parent refers to it's extending class; which is mParent. mChild now has access to mParent's methods and attributes. If we changed everything from the methods and attributes to access types of private, mChild could no longer access anything.
If we added a attribute called 'car' in the mParent class and assign the value of 'Peugeot 306', and make a method called drive() would you want your child to have 'access' to it?
Code:
class mParent {
protected static $firstname = 'Brown';
protected static $secondname = 'Cow';
private static $car = 'Peugeot 306';
public function myName() {
echo "My name is " . self::$firstname . " " . self::$secondname . ".";
return true;
}
private function driveCar() {
echo "I am going to drive my " . self::$car . " off a cliff.";
return true;
}
}
class mChild extends mParent {
protected static $firstname = 'Yellow';
protected static $secondname;
public function myName() {
self::$secondname = parent::$secondname;
echo "<br/>My name is " . self::$firstname . " " . self::$secondname . ".";
return true;
}
}
$Parent = new mParent;
$Parent->myName();
$Child = new mChild;
$Child->myName();
N... well, that is up to you. The child could not access the method drive() or the attribute $car.
So, let's go over what we have just worked with.
The access modifiers:
public,
private and
protected dictate what the object has access to.
The keyword
static allows the class to access it using late static binding, the keywords
self and
parent. So, the class does not need to be instantiated before hand.
What's next?In the next tutorial, we will be looking at late static binding, focusing on the keywords:
static,
self, and
parent.
This concludes the restricting access using access modifiers tutorial.
Please rate the tutorial, so I can get an idea of how good my tutorial was.
Thanks for reading!
_________________
Remember to
search the forums, and external resources such as
Google, before posting your
problem. I am open to answer your questions, if you private message me.

Created by
Will