InterviewSolution
| 1. |
What Are Different Visibility Of Method/property? |
|
Answer» There are 3 TYPES of visibility of method & property and are following Public: Can be accessed from same CLASS method, child class and from outside of class. Protected : Can be accessed from same class method, child class. PRIVATE: Can be accessed from same class method only. class TestClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printValue() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new TestClass(); echo $obj->public; // Works echo $obj->protected; // Fatal error: Cannot access protected property TestClass::$protected in echo $obj->private; // Fatal error: Cannot access private property TestClass::$private in C:wampwwwarunclassclass.php on line 20 $obj->printValue(); // Shows Public, Protected and Private There are 3 types of visibility of method & property and are following Public: Can be accessed from same class method, child class and from outside of class. Protected : Can be accessed from same class method, child class. Private: Can be accessed from same class method only. class TestClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printValue() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new TestClass(); echo $obj->public; // Works echo $obj->protected; // Fatal error: Cannot access protected property TestClass::$protected in echo $obj->private; // Fatal error: Cannot access private property TestClass::$private in C:wampwwwarunclassclass.php on line 20 $obj->printValue(); // Shows Public, Protected and Private |
|