note 123687 added to language.oop5.visibility

From: Date: Tue, 12 Mar 2019 19:54:07 +0000
Subject: note 123687 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-210365@lists.php.net to get a copy of this message
1 . If the class member declared as public then it can be accessed everywhere. 2 . If the class members declared as protected then it can be accessed only within the class itself and by inheriting and parent classes. 3 .If the class members declared as private then it may only be accessed by the class that defines the member. <?php // BaseClass class pub { public $tag_line = "A Computer Science Portal for Geeks!"; function display() { echo $this->tag_line."<br/>"; } } // SubClass class child extends pub { function show(){ echo $this->tag_line; } } // Object Declaration $obj= new child; // A Computer Science Portal for Geeks! echo $obj->tag_line."<br/>"; // A Computer Science Portal for Geeks! $obj->display(); // A Computer Science Portal for Geeks! $obj->show(); ?> <?php // Base Class class pro { protected $x = 500; protected $y = 500; // Subtraction Function function sub() { echo $sum=$this->x-$this->y . "<br/>"; } } // SubClass - Inherited Class class child extends pro { function mul() //Multiply Function { echo $sub=$this->x*$this->y; } } $obj= new child; $obj->sub(); $obj->mul(); ?> <?php // Base Class class demo { private $name="A Computer Science Portal for Geeks!"; private function show() { echo "This is private method of base class"; } } // Sub Class class child extends demo { function display() { echo $this->name; } } // Object Declaration $obj= new child; // Uncaught Error: Call to private method demo::show() $obj->show(); //Undefined property: child::$name $obj->display(); ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.142 (proxied: 103.84.255.197) ---- Manual Page -- http://php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/123687 Del: integrated -- https://master.php.net/note/delete/123687/integrated Del: useless -- https://master.php.net/note/delete/123687/useless Del: bad code -- https://master.php.net/note/delete/123687/bad+code Del: spam -- https://master.php.net/note/delete/123687/spam Del: non-english -- https://master.php.net/note/delete/123687/non-english Del: in docs -- https://master.php.net/note/delete/123687/in+docs Del: other reasons-- https://master.php.net/note/delete/123687 Reject -- https://master.php.net/note/reject/123687 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#210365) next »