Register    Login    Forum    Search    FAQ

Board index » Official » Tutorials




 Page 1 of 1 [ 1 post ] 




Did you like the tutorial? Please rate it!
1 0%  0%  [ 0 ]
2 0%  0%  [ 0 ]
3 0%  0%  [ 0 ]
4 50%  50%  [ 1 ]
5 50%  50%  [ 1 ]
Total votes : 2
 
Author Message
 Post subject: [PHP] Learning OOP: Restricting a... | Part 2 [28 July 2010]
 Post Posted: Wed Jul 28, 2010 6:15 pm 
Offline
Site Administrator
Site Administrator
User avatar

Joined: Sun May 16, 2010 12:33 am
Posts: 961
Location: England
Learning OOP: Restricting access using access modifiers Part 2/2
27, 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 child
One 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.

Image Created by Will
Image


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 1 post ] 




Board index » Official » Tutorials


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: