Modifier = visibility = Access Level of data member or Member function.
Modifier play very vital rule in any class.
Becuase
Some property all can acess - Public
Some property only relevant person can acess - Protected
Some property only you can access - Private
Therefore Modifiers are three types
a) Public
b) Protected
c) Private
Data member can be three types public, protected OR private
Member function can be three types public, protected OR private.
Data Member refer to class variable.
Member function refer to class function.
Modifier play very vital rule in any class.
Becuase
Some property all can acess - Public
Some property only relevant person can acess - Protected
Some property only you can access - Private
Therefore Modifiers are three types
a) Public
b) Protected
c) Private
Data member can be three types public, protected OR private
Member function can be three types public, protected OR private.
Data Member refer to class variable.
Member function refer to class function.
Example1:
class MyClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // It will be Works echo $obj->protected; // Fatal Error becuase you cannot access protected in this way echo $obj->private; // Fatal Error becuase you cannot access protected in this way $obj->printHello(); // Shows Public, Protected and Private data meber values
Example 2:
class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // It will be Work echo $obj2->private; // Undefined becuase private variable can not be inherit echo $obj2->protected; // Fatal Error becuase protected can be used within function only $obj2->printHello(); // Shows Public, Protected2, Undefined
No comments:
Post a Comment