Visibility Keywords in PHP 5- Public,Private and Protected

Posted By: Matpal - March 14, 2011
PHP 5 includes visibility keywords to make its OOP model more effective and significant. Where in PHP 4 the class structure was straight forward like -

class ExamplePHP4
{
var $name;
var $addr;
var $salary;
}


Using visibility keywords PHP5 is using OOP model in an efficient way. In PHP5 we can use following visibility keywords-

The visibility of a property or method can be defined by prefixing the declaration with the the visibility keywords;
  • public - The variable can be accessed and changed globally by anything.
  • private - The variable can be accessed and changed only from within the class.
  • protected - The variable can be accessed and changed only by direct descendants (those who inherit it using the extends statement) of the class and class its self)
This example explains it better-


class ExamplePHP5
{
public $name;
private $addr;
protected $salary;
}




0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.