Restricting Access to Properties and Methods


You can access the methods and properties of an object by default, but sometimes that's not a good idea. For example, consider the accessor method set_name from the previous chunk, which restricts access to the name property:

 function set_name($text) {     if (strlen($text) <= 128){         $this->name = $text;     } } 

It would be a good idea to make sure that no one can access the name property directly from outside the object. Currently, it's no problem to do that, using this syntax:

 $lion = new Animal; $lion->set_name("Leo"); echo "The name of your new lion is ", $lion->name, "."; 

But it turns out that you can make the name property private to the Animal class if you want. You do that with the PHP access modifiers:

  • public. Means "Accessible to all"

  • private. Means "Accessible in the same class"

  • protected. Means "Accessible in the same class and classes derived from that class"

By default, all methods and properties are declared public, which means there's no restriction on accessing them from outside an object. But if you declare the name property as private, using the private keyword, that property can't be accessed from code outside the class itself:

 <?php     class Animal     {         private $name;         function set_name($text)         {$this->name = $text;}         function get_name()         {return $this->name;}     }     $lion = new Animal;     $lion->set_name("Leo");     echo "The name of your new lion is ", $lion->name, "."; ?> 

Now when you try to access this private property as $lion->name, you'll get an error:

[View full width]

The name of your new lion is PHP Fatal error: Cannot access private property Animal: :$name in lion.php on line 20

Can you make methods private too? You sure can, as when you want to make a method entirely internal to a class. Here's how we make the get_name method private and then try to use it:

 <?php     class Animal     {         var $name;         function set_name($text)         {$this->name = $text;}         private function get_name()         {             return $this->name;         }     }     $lion = new Animal;     $lion->set_name("Leo");     echo "The name of your new lion is ", $lion->get_name(), "."; ?> 

And here's what you see:

 The name of your new lion is PHP Fatal error: Call to private method Animal::get_name() from context '' in C:\php\t.php on line 21 

By default, everything in a class is public, which means it's accessible from outside the object (you can also use the public keyword to make that explicit). The protected keyword is used when one class is used as a base class of another, as we're going to see later in this chapter. Properties and methods made protected in a base class may only be used in that class and in any class derived from that class. More on how that works in a few pages.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net