Is it possible to instantiate a class without calling the constructor in PHP?

Asked

Viewed 796 times

6

It is possible to instantiate a PHP class without the class calling the method __construct?

Example:

class Test {

     public $chamado = false;

    public function __construct()
    {
          $this->chamado = true;
    }
}


$teste = new Test;

var_dump($teste->chamado); //bool(true);

How to call this class without chamado be moved to true, for example?

There is this possibility in PHP?

  • 1

    Yes it is possible, believe me!

  • 1

    It would be simpler if you didn’t define a method construct.

  • That’s what Ivan said. I also agree. But anyway, it’s worth the curiosity ;)

  • If you think the title has gone bad do the rollback please.

  • 1

    The method you have specified below will cause some problems for some people who do not understand the use of constructors, it would be good if you explained a reason and when to do this.

2 answers

4

Through the class of class reflection ReflectionClass can yes do this in php.

Example:

$reflect = new ReflectionClass('Test')

$test = $reflect->newInstanceWithoutConstructor();

var_dump($test->chamado); // bool(false);

2

The constructor method is not required to create an instance of the class, it is only done to set the initial values when creating its instance. This occurs when it invokes the constructor, the constructor can be defined in two ways:

Thus:

class SuaClasse
{
  private $chamado = false;

  public function __construct()
  {
    //o que será construído junto à instância
      $this->chamado = true;
  }

  public function setChamado($boolean)
  {
     $this->chamado = $boolean;
     return $this;
  }

  public function getChamado()
  {
     return $this->chamado;
  }
} 

or so:

class SuaClasse
{
  private $chamado = false;

  public function SuaClasse()
  {
    //o que será construído junto à instância
    $this->chamado = true;
  }

  public function getChamado()
  {
     return $this->chamado;
  }
} 

There is also the destructive method:

class SuaClasse
{
  private $chamado = false;

  public function __destruct()
  {
    //o que será destruído junto à instância
    $this->chamado = true;
  }
} 

You don’t necessarily need to create a class constructor method in the instance, you also don’t have to worry about the constructor method setting its output:

$suaClasse = new SuaClasse;

You can set the values:

//será false
$suaClasse->setChamado(false);
echo $suaClasse->getChamado();
//será true
$suaClasse->setChamado(true);
echo $suaClasse->getChamado();

Directly, to call the builder, would be so:

 $suaClasse = new SuaClasse();
 //será true
 echo $suaClasse->getChamado();

Another way to make the builder work for his needs is to define how he will start:

class SuaClasse
{
  private $chamado = false;

  public function __construct($boolean)
  {
    //o que será construído junto à instância
      $this->chamado = $boolean;
  }

  public function getChamado()
  {
     return $this->chamado;
  }

} 

$suaClasse = new SuaClasse(true);
//será true
$suaClasse->getChamado();

$suaClasse = new SuaClasse(false);
//será false
$suaClasse->getChamado();
  • 1

    I understand your vision. But here it is more related to the declaration of the class than to the instantiation without calling the __construct. Nor does it make sense for anyone to want to instantiate a class without the constructor (at least no such case comes to mind)+1

  • 2

    Just a detail, about the construction style php4, in php7 this style will return a Warning E_DEPRECATED, in php5 if the class is inside a namespace the method of the same name(constructor php4) will be treated as a normal method, as stated in documentation and +1 for the good answer.

  • 1

    By the way, PHP 7 was officially released yesterday.

  • You speak of the class name as method... for it is, so it is good to avoid it.

  • @Ivanferrer, I agree it’s good to avoid anyway, so you’ll get the hang of it when you get php7 :P, see how long the guys use deprecated functions after they are removed is just crying.

Browser other questions tagged

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