Why can’t I declare an attribute as an object?

Asked

Viewed 129 times

12

I have a class A and I’m creating a class B. I want one of the attributes of B is an object of A.

Why the notation public $objeto = new A(); is not correct?

  • 1

    You know the concept of memory allocation?

  • 4

    The language does not allow this type of 'complex' startup. Remember that php is interpreted. Related: Instantiation of objects in php

  • @Andersoncarloswoss I don’t have much knowledge of this part of the programming. Basically I only know the part of the surface layer.

  • In fact, most languages pass this initialization into the constructor, simply allowing it by syntactic simplification, such as C# or Java.

2 answers

12


Why the public notation $object = new A(); is not correct?

Because language doesn’t allow it. As says the documentation, the initialization must be the value of a constant, that is, it must be possible to know it at compile time. If this value depends on a function call, that is, an expression, it cannot depend on anything that is known at runtime (Runtime).

This declaration may include an initialization, but this initialization must be a Constant value-that is, it must be Able to be evaluated at Compile time and must not Depend on run-time information in order to be evaluated.

As the other answers commented, the solution for these 'complex' initializations is to create a constructor of its own.

This code is invalid as the expression depends on the execution/call/class creation at runtime.

class teste{
    static $a;
    static $b = 10;
    static $total = self::$a + self::$b;
}

But that instruction works:

class teste{
    static $total = 10 + 51;
}

Recommended reading:

What good is a builder?

  • 5

    Ah, do you have that too? So it’s so easy to let it go like that and carry it to the constructor automatically, like other languages do. And even if it is interpreted it is necessary to evaluate the whole class anyway. At least no one can deny my comment because I notice that PHP has more incompetence :D

  • In PHP when I do this: public $obj = new MinhaClasse(); the expression new MinhaClasse(); can be considered a function? From what I understand, it would not work only for functions public $obj = pegaValor();.

  • 1

    @cat the constructor is a 'special function' so it needs information that is not known at compile time.

  • @Mustache negative nobody can at most signal as offensive! 11 :D. I imagine it’s a little expensive to do this or it just doesn’t make up for it since the life of the script is short.

  • @rray an array used to initialize the property of a class like this: public $palavras = ["gato", "meow"]; could be considered a constant ?

  • 2

    @cat that there yes, the values of the array elements are known at 'compile' time in this case pq are fixed. It is constant in the sense that the value will not change during 'compilation' after the script execution it can change yes.

  • 3

    @rray ie, is scripting language without the facilities of being a script :D

  • @rray I did a test here with the function array() to initialize the field with an array: public $palavras2 = array("gato", "meow"); and allowed to use the function array(). Why the function array() can be used? What is special about it in relation to other functions?

  • 2

    @pussycat array() is not a function it is a language constructor i.e., this instruction is part of the language, this staff has its own rules (exceptions) :P. More details about the array() => Here. To get an idea I recommend reading the description of print. They changed it a little before it was something like: "print is not a function but works as a function except for not needing () on the call.

Show 4 more comments

2

Just a complement to the other answer, you can only define values as variables, class instances, and function calls to a class if you use the __constructor method.

In your case.

class B{

   protected $a;

   // Determina que o parâmetro passado será armazenado em 'a'
   // e deve ser uma instância da classe `A`.
   public function __construct (A $a) {
          $this->a = $a;
   }

   public function getA()
   {
       return $this->a;
   }
}

$a = new A;
$b = new B($a);

In some cases, because instances of certain classes need to be added to one another, the pattern of addiction injection

Browser other questions tagged

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