How to determine if a class property is private or protected

How to determine if a class property is private or protected without using external libraries (PHP only)? How can I check if I can set a property outside the class or cannot?

+4
source share
3 answers

Use Reflection.

 <?php class Test { private $foo; public $bar; } $reflector = new ReflectionClass(get_class(new Test())); $prop = $reflector->getProperty('foo'); var_dump($prop->isPrivate()); $prop = $reflector->getProperty('bar'); var_dump($prop->isPrivate()); ?> 
+8
source

Using:

print_r($object_or_class_name);

He should bring it out for you, properties that you can or cannot access.

For instance:

 class tempclass { private $priv1 = 1; protected $prot1 = 2; public $pub1 = 3; } $tmp = new tempclass(); print_r($tmp); exit; 

Just to illustrate that I have one private property, one protected property and one public property. Then we see the output print_r($tmp); :

 tempclass Object ( [priv1:tempclass:private] => 1 [prot1:protected] => 2 [pub1] => 3 ) 

Or I do not understand your post? Haha

0
source

Source: https://habr.com/ru/post/1385545/


All Articles