Is that a bug in stdClass?

Asked

Viewed 145 times

3

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

Let me give you 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";

But when we do it in stdClass, something unexpected happens.

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

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

Note: To prove I’m not making it up, here comes the code on IDEONE

Updating: Because the posted answers talk about have to do with the parameters in the class or function constructor, or not so that the assignment happens when we assign the value to a variable that at the same time we pass as parameter, I am putting an example 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 meter in the constructor, as in the case of stdClass
  • And lastly, we have the 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 the IDEONE, the results were respectively:

a
b
Undefined variable 'c'

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

  • 2

    To prove I’m not making it up... :D

3 answers

6


I don’t know how PHP handles 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 the object instantiation.

Example

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

class bb {
}

new aa($a = []);

var_dump($a);

new bb($b = []);

var_dump($b);
  • 1

    If I have a class that does not accept parameters in the constructor, the assignment will happen correctly. I will post an example!

  • See somehow when we define a constructor, the attribution is valid, but when we do not define the constructor the attribution is not.

  • 1

    The problem then is not the question of parameterization. It was understood by his example that when we instantiate the class, the constructor is not called (because there is no constructor). So the PHP interpreter does not create the variable internally because of this. That is, the question is not the constructor without parameters, is that, if it does not exist, the PHP interpreter does not process what is passed by parameter in the stdClass or any other class without __construct. Legal :)

  • 1

    Now +1 was more than deserved. The answers were not wrong, but it was necessary to explain that __construct() no parameter is not the same thing as not having the __construct. Madness, madness, madness, madness!

  • 2

    PHP has of these things 1 = true, null = false...

2

stdClass does not accept parameters.

The best way to define a value like this for 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.

  • What is being discussed here is that: If PHP accepts to declare variables at the same time as we pass it by parameter, why is it not so in stdClass (in any other class, it’s like this)

  • 1

    Detail,, stdClass has a specific way of being used which means that even if you pass any parameters to it, it disregards, even if it is you setting a variable. this has never worked and there is no prediction that this class will do that, so yes, this is a specific case that breaks the general rules of the PHP. and it is not the only case that the php breaks its own rules. : D

  • +1 for editing the answer and make it clearer, your answer is good, I do it this way too.

  • You still don’t understand the meaning of the question

  • @Erloncharles, you cast a cast, to convert array for object (Or the stdClass). The answer still has nothing to do with the question I asked. I did not ask about how the instance of stdClass, but because this behavior is only present in him. We are not dealing with the subject "instance", but rather "variables defined at the same time as passed with the given parameter"

  • But your question is specifically "Is this a bug in stdClass?". So there is rather an answer to your question in the first and last line of my answer.

Show 1 more comment

1

To further strengthen our theory regarding the question, I will give my opinion on the subject.

As exemplified by our friend, the question is not whether you have a constructor or function with or without parameters. This does not affect anything in relation to the variable passed by argument.

The examples below will cause the variables to be assigned:

function a(){}

class b
{
   public function __construct(){}
}

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

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

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

class C{}

new C($c = 3);

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

This is a question that is probably related to the PHP interpreter. If the class has no constructor, then there is no need to process what is passed as class instance parameter - maybe they thought so when they did the encoding of the classes.

The last thing we have to do then is to check whether the stdClass really doesn’t own the __construct internally - only for 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 exit is :

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

Then it is proven that there is no __construct in class stdClass

  • Then you can also do things like this, that’s why I like PHP :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.