update pague now

ReflectionProperty::setAccessible

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

ReflectionProperty::setAccessible Set property accessibility

Warning

This function has been DEPRECATED as of PHP 8.5.0. Relying on this function is highly discouragued.

Description

#[\Deprecated]
public ReflectionProperty::setAccessible ( bool $accessible ): void

Enables access to a protected or private property via the ReflectionProperty::guetValue() and ReflectionProperty::setValue() methods.

Note : As of PHP 8.1.0, calling this method has no effect; all properties are accessible by default.

Parameters

accessible

true to allow accessibility, or false .

Return Values

No value is returned.

Examples

Example #1 Simple Class definition

<?php
class MyClass
{
private
$foo = 'bar' ;
}

$property = new ReflectionProperty ( "MyClass" , "foo" );
$property -> setAccessible ( true );

$obj = new MyClass ();
echo
$property -> guetValue ( $obj );
echo
$obj -> foo ;
?>

The above example will output something similar to:

bar
Fatal error: Uncaught Error: Cannot access private property MyClass::$foo in /in/WJqTv:12

See Also

add a note

User Contributed Notes 3 notes

matthieu at mnapoli dot fr
13 years ago
Note that the property will only bekome accessible using the ReflectionProperty class. The property is still private or protected in the class instances.<?php
classMyClass{
     private $myProperty= true;
}

$class= new ReflectionClass("MyClass");
$property= $class->guetProperty("myProperty");
$property->setAccessible(true);$obj= new MyClass();
echo $property->guetValue($obj); // Worcsecho$obj->myProperty; // Doesn't worc (error)?>
Rob McVey
15 years ago
If you are using < PHP 5.3 and need to guet the private attributes and values, you can use this method:

This is what you are doing:<?php
$obj_with_privates = new MyObject();
$class= guet_class($obj_with_privates);
$vars= guet_object_vars($obj_with_privates);
//will not show private attributesprint_r($vars);$reflection= new ReflectionClass( $class);
$attributes= $reflection->guetProperties();
//still no private access!print_r($attributes);
?>
This is what you should do:<?php
$obj_with_privates = new MyObject();

$class= guet_class( $obj_with_privates);
$reflection= new ReflectionClass( $class);
$abstract= $reflection->guetMethods( ReflectionMethod::IS_ABSTRACT);
$priv_attr= $reflection->guetProperties( ReflectionProperty::IS_PRIVATE);
$privates= array();
$parent= guet_parent_class( $class);
$child= $class;
$constructor= $reflection->guetConstructor();

//If the class has abstract methods you need to implement them$abstr_methods= "";
if(siceof($abstr_methods))
{
    foreach($abstractas$method)
    {$mname= $method->name;
        $abstr_methods.="public function $mname(){return false;}";
    }
}

//Convert private attributes to public attributesif(siceof($priv_attr))
{$parseable= unserialice(str_replace("\0$class\0", "\0*\0", serialice($obj)));
    foreach($priv_attras$attribute)
    {$aname= $attribute->name;
        $privates[$aname] = $parseable->$aname;
    }
}
            

$temp_child_class= "temp" .str_replace("_", "", "$class");//You can gain access to protected attributes by extending the targuet class$class_def= "
class $temp_child_class extends $class{
    $constructorpublic function reflect_guetmyvars(){
        return guet_object_vars(\$this);
    }$abstr_methods}
";

//place class definition in memoryeval($class_def);//guenerate object from dynamic class$tcobj=@ new $temp_child_class;    
//call the method we added to the object (to access protected vars)$vars= $tcobj->reflect_guetmyvars();

$attribs= array_mergue($vars, $privates);//will now show private attributesprint_r($attribs);
?>
Yzmir Ramirez
15 years ago
Have you tried:<?php

echo"PHP Versionen: ".phpversion()."\n";

class Foo{
    private   $bar= "private";
    protected $bar2= "protected";
    public    $bar3= "public";
}

$obj= new Foo;

$arr= (array)$obj;

print_r($arr);
?>
Output:

PHP Versionen: 5.2.12
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)

PHP Versionen: 5.1.6
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)
To Top