Is this a bug in stdClass?

3

In PHP it is possible to declare a variable at the same time that we pass it as a parameter of a function or in the instantiation of the class.

I'll give an example with the function is_file and the class ArrayObject .

$arrayObject = new ArrayObject($a = []);

var_dump($a); // Imprime: array(0) {}


is_file($b = 'index.php');

print_r($b); // Imprime: "index.php";

However, when we do this in stdClass , something unexpected happens.

$object = new stdClass($c = []);

print_r($c); //Undefined variable 'c'

Note : To prove that I'm not inventing, here's the code in IDEONE

Update : Because the posted responses speak about having to do with the parameters exist in the class or function constructor, or not for assignment to occur when we assign the value to a variable that at the same time we pass as parameter, I am putting a sample code to prove that this is not true.

I created three scenarios.

  • The first class ComParametro accepts only one parameter in the constructor
  • The second class SemParametro does not accept any parameters in the constructor, as in the case of stdClass
  • Finally, we have stdClass .

Let's see:


class ComParametro
{
   public function __construct($parametro){}
}


class SemParametro
{
    public function __construct(){}
}

new ComParametro($a = 'a');


print_r($a);

new SemParametro($b = 'b');

print_r($b);

new stdClass($c = 'c');

print_r($c);

As we can see in IDEONE , the results were respectively:

a
b
Undefined variable 'c'

So what I want to know is why this behavior is present in stdClass !

    
asked by anonymous 04.08.2015 / 15:28

3 answers

6

I do not know how PHP treats this case, but if a class has not defined the constructor (which is the case stdClass ), then any assignment will be invalid at the time of instantiation of the object.

Example

<?php
class aa {
  function __construct() {}
}

class bb {
}

new aa($a = []);

var_dump($a);

new bb($b = []);

var_dump($b);
    
04.08.2015 / 16:31
2

stdClass does not accept parameters.

The best way to set a value like this to sdtClass is a conversion:

$std = (object)$c = [];

var_dump($std); // Retorna object(stdClass)#1 (0) {}
var_dump($c); // Retorna array(0) {}

So NO, this is the definition of the class and not a bug.

    
04.08.2015 / 15:55
1

To further strengthen our theory regarding the question, I will give my pitaco about the subject.

As exemplified by our friend, the point is not to have a constructor or function with or without parameters. This does not affect anything relative to the variable passed by argument.

The examples below will assign variables:

function a(){}

class b
{
   public function __construct(){}
}

a($a = 1);
new b($b = 2);

var_dump($a, $b); // Imprime 1 e 2

In the case of the constructor not being defined, the problem described in the question occurs:

class C{}

new C($c = 3);

var_dump($c); // Undefined variable 'c'

This is an issue that is probably related to the PHP interpreter. If the class has no constructor, then there is no need to process what is passed by the instance instance of the class - maybe they thought so when they coded the classes.

The last thing we have to do is check that stdClass does not really have __construct internally - just for the means of concrete evidence.

So, come on:

$object = new stdClass;

$reflector = new ReflectionClass($object);

var_dump($reflector->getConstructor()); // NULL

Already in ArrayObject

$reflector = new ReflectionClass(new ArrayObject());

var_dump($reflector->getConstructor());

The output is:

class ReflectionMethod#4 (2) {
  public $name =>
  string(11) "__construct"
  public $class =>
  string(11) "ArrayObject"
}

Then it is proven that __construct does not exist in class stdClass

    
04.08.2015 / 18:01