0
class A
{
   private $x=100;
   private $y=200;

}
$a=new A();

$x=(array) $a;

foreach($x as $key=>$val)
{
  echo $x[$key];
}

I have issue with the private variable of Class A.

Class A private variable access outside the class when I do typecasting object to array. It should not be access outside the class. But above example I can access private variable of class A.

Here is the result

100200

How can I resolve this issue?

14
  • 2
    Reflections can do that too. Just saying... Commented Jul 12, 2016 at 11:51
  • 2
    Is there a particular reason for you to want to block that? Commented Jul 12, 2016 at 11:52
  • 3
    Have a read of this interesting article Commented Jul 12, 2016 at 11:53
  • 6
    The purpose or private members is not privacy, but rather to ensure the class functions properly and no outside intervention can break intended behaviour. Since the manipulation can only occur on an array and not on class objects themselves then the principle is not violated. Commented Jul 12, 2016 at 11:54
  • 1
    What is the purpose of this question? There are a metric ton of ways in which "private" properties can be accessed. If you're concerned about "security", you're betting on the wrong horse. If you have another motive, spell it out, please. Commented Jul 12, 2016 at 11:54

4 Answers 4

4

First of all I'll start with, the purpose of having private members in an object is to ensure the object itself can have some parts that it has absolute control of internally and can rely on those parts for certain behaviours. It is not to ensure privacy of the members since there are ways for them to be accessed in all languages (even if PHP makes it really easy).

You can make your own function which only exposes public properties if you want to:

function toAccessibleElementArray($object) {
    if (!is_object($object)) { return []; } //or other sensible default?
    $reflectionClass = new ReflectionClass(get_class($object));
    $array = [];
    foreach ($reflectionClass->getProperties() as $property) {
        if ($property->isPublic()) {
             $array[$property->getName()] = $property->getValue($object);
        }
    }
    return $array;
}
Sign up to request clarification or add additional context in comments.

1 Comment

If anyone uses this, make sure to do an is_object on $object before running this. Because get_class(null) will return the current class, which will give REALLY strange results for this method :)
1

PHP 7.4

<?php
class A
{
   private $x=100;
   private $y=200;

}
$a=new A();

$m = function(){
    return [$this->x, $this->y];
};

[$x ,$y] = $m->call($a);

echo $x.','.$y;

You're not accessing private members there

<?php
class A
{
   private $x=100;
   private $y=200;
   private function __construct(){}
   public function setX(){
       $this->x =100;
   }
   public function setY(){
       $this->y =200;
   }
}
$a=new A();

$m = function(){
    return [$this->x, $this->y];
};
[$x ,$y] = $m->call($a);
echo $x.','.$y;
<b>Fatal error</b>:  Uncaught Error: Call to private A::__construct() from invalid context in 

Comments

0

You're not accessing private members there. All you've got is an array holding the state of the object. Encapsulation is preserved, you're not allowed private member manipulation outside the class blocks.

Now you're allowed to bend over backwards and get the object state which you can use to do whatever, but that's just a poorly written client.

There's only so much a language can do, you should be free to write good/bad code in any language.

Comments

0

It's a documented behaviour:

http://www.php.net/manual/en/language.types.array.php

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.