note 112768 added to language.oop5.visibility

From: Date: Sun, 21 Jul 2013 14:08:15 +0000
Subject: note 112768 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-195695@lists.php.net to get a copy of this message
As far as it regards the properties of objects, visibility is, yes, as the examples show. Private, protected methods are not accessible via syntax $a->protectedVar, however their values are still (php 5.3.26) accessible through a number of other methods (serializing, converting to array, and nevertheless using the ReflectionClass methods). As it was pointed out and such as in the example below: <?php echo "PHP Version: ".phpversion()."\n"; class Foo { private $bar = "value of private var"; protected $bar2 = "value of protected var"; public $bar3 = "value of public var"; } $obj = new Foo; echo serialize($obj) . "\n"; print_r($obj); print_r((array)$obj); echo ($obj->bar3) . "\n"; echo ($obj->bar2) . "\n"; echo ($obj->bar) . "\n"; ?> It will output: PHP Version: 5.3.26 O:3:"Foo":3:{s:8:"Foobar";s:20:"value of private var ";s:7:"*bar2";s:22:"value of protected var";s:4:"bar3";s:19:"value of public var";} Foo Object ( [bar:Foo:private] => value of private var [bar2:protected] => value of protected var [bar3] => value of public var ) Array ( [Foobar] => value of private var [*bar2] => value of protected var [bar3] => value of public var ) value of public var While the last two lines, accessing directly the private and protected object properties (bar2 and bar), will throw out fatal errors like: PHP Fatal error: Cannot access private property Foo::$bar and PHP Fatal error: Cannot access protected property Foo::$bar2 ---- Server IP: 69.147.83.199 Probable Submitter: 86.126.3.157 ---- Manual Page -- http://php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/112768 Del: integrated -- https://master.php.net/note/delete/112768/integrated Del: useless -- https://master.php.net/note/delete/112768/useless Del: bad code -- https://master.php.net/note/delete/112768/bad+code Del: spam -- https://master.php.net/note/delete/112768/spam Del: non-english -- https://master.php.net/note/delete/112768/non-english Del: in docs -- https://master.php.net/note/delete/112768/in+docs Del: other reasons-- https://master.php.net/note/delete/112768 Reject -- https://master.php.net/note/reject/112768 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195695) next »