Instantiation of objects in php

Asked

Viewed 108 times

2

I’m having 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 'Testeusuario' (T_STRING) in C: wamp www lists Testeusuario.php on line 4 >

If instantiating outside also gives error.

How do I do it? I’ve already realized that I can use the test file without class, i.e.:

 include("Usuario.php");  

 $usuario2 = New Usuario();  
  • 1

    It is unlikely that I wanted to actually do this. Doing out works perfect if I use the correct syntax. There are several errors in this code. Doing inside the class is what has already been answered, but to create an instance of the class itself in its constructor does not give.

  • @bigown what are code errors? Grateful.

3 answers

4

In php it is not possible to store instances directly in the declaration of properties, as is done 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 define public, protected or private for properties. In older versions one could define var, but this feature is discouraged from version 5 of php.

Behold:

classe TesteUsuario {  

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

The correct form would be:

classe TesteUsuario {  

    public $usuario1;
    protected $usuario2;
 }

References:

4

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

In place of:

classe TesteUsuario {  

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

Modify to:

classe TesteUsuario {  
  private $usuario1;
  private $usuario2;

  public function __construct(){
    $this->usuario1 = TesteUsuario();  
    $this->usuario2 = New Usuario();  
  }
} 
  • this time who was faster?

  • @rray And even not assigning data to a property or attribute, as I should do instantiation, since it does not allow within the scope of the class?

  • @Has Andrénascimento managed to solve? Something is missing?

  • @rray I managed to do without the second class. I only kept the User class and the Testeusuario class I excluded. When I prompt the User in the file that would be the Testeusuario class works. But I can’t do the scrolling and instantiating the object as down cast. I have noticed that there is some difference in relation to java.

  • For those who are in doubt in what I want to do, it’s no big deal. I’m just studying php even.

  • @Andrénascimento blz so I didn’t take long to look at the question, then I found something pending.

Show 1 more comment

0

I understood what you wanted to do, but I didn’t understand the reason or need. See if that’s 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 attributes, you will need a constructor for this.

I passed the above solution, however, in my understanding this use is not "correct", or at least does not represent a use within the common understanding.

Browser other questions tagged

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