Instantiation of objects in php

2

I have some difficulties in instantiating objects in php.

I have the code in OO:

class Usuario {  

    private $idade;  
    private $nome;  

    public function getNome() {  
        return $this->nome;  
    }  

    public function setNome($nome) {  
        $this->nome=$nome;  
    }  

    public function getIdade() {  
        return $this->idade;  
    }  

    public function setIdade($idade) {  
        $this->idade=$idade;  
    }  

}

And the test class:

include("Usuario.php");  

classe TesteUsuario {  

    $usuario1 = TesteUsuario();  
    $usuario2 = New Usuario();  
 }  

Instantiating the object within the scope of the class, gives the error:

  

(!) Parse error: syntax error, unexpected 'TestUser' (T_STRING) in C: \ wamp \ www \ lists \ TestUser.php on line 4>

If instantiating outside also gives error.

How do I? I have already realized that I can use the test file without class, ie:

 include("Usuario.php");  

 $usuario2 = New Usuario();  
    
asked by anonymous 20.09.2016 / 21:22

3 answers

4

In php it is not possible to store instances directly in the declaration of properties, as it does in C# .

You can solve the problem by doing this in the constructor.

class TesteUsuario {

     protected $usuario;

     public function __construct() {
         $this->usuario = new Usuario;
     }
}

Another thing I noticed is that you did not declare the visibility of the properties. In PHP it is necessary to set public , protected or private to properties. In older versions you could set var , but this feature is discouraged from version 5 of php.

See:

classe TesteUsuario {  

    $usuario1 = TesteUsuario();  
    $usuario2 = New Usuario();  
 }

The correct form would be:

classe TesteUsuario {  

    public $usuario1;
    protected $usuario2;
 }

References:

20.09.2016 / 21:27
4

You can not assign directly created objects to a property or function joke. In php this should be done in the constructor. Only fixed values and some types of expressions are allowed.

Instead of:

classe TesteUsuario {  

    $usuario1 = TesteUsuario();  
    $usuario2 = New Usuario();  
 } 

Change to:

classe TesteUsuario {  
  private $usuario1;
  private $usuario2;

  public function __construct(){
    $this->usuario1 = TesteUsuario();  
    $this->usuario2 = New Usuario();  
  }
} 
    
20.09.2016 / 21:28
0

I understood what you want to do, but I did not understand the reason or need. See if this would be it:

<?php
class Usuario {  

    private $idade;  
    private $nome;  

    public function getNome() {  
        return $this->nome;  
    }  

    public function setNome($nome) {  
        $this->nome=$nome;  
    }  

    public function getIdade() {  
        return $this->idade;  
    }  

    public function setIdade($idade) {  
        $this->idade=$idade;  
    }  

}

Here's what was "wrong":

<?php
include("usuario.php");  

class TesteUsuario {  

    private $usuario1;
    private $usuario2;

    function __construct($instancia = null)
    {
        $this->usuario1 = $instancia;
        var_dump($this->usuario1);
        $this->usuario2 = new Usuario; 
    }
 }

 new TesteUsuario(new TesteUsuario);

In PHP you will not be able to assign an object created in the definition of the attributes, you will need a constructor for this.

I came up with the above solution, but in my understanding this usage is not "correct", or at least does not represent a use within the common understanding.

>     
20.09.2016 / 21:50