Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling class method (with constructors) without object instantiation in php

Ive looked and tried but I can't find an answer.

In PHP, is it possible to call a class' member function (when that class requires a constructor to receive parameters) without instantiating it as an object?

A code example (which gives errors):

<?php

class Test {
    private $end="";

    function __construct($value) {
        $this->end=$value;
    }

    public function alert($value) {
        echo $value." ".$this->end;
    }
}

//this works:
$example=new Test("world");
$example->alert("hello");

//this does not work:
echo Test("world")::alert("hello");

?>
like image 219
myk00 Avatar asked Jun 23 '10 04:06

myk00


People also ask

How can we call a method without creating object in PHP?

Example Explained Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).

Is it possible to call a constructor manually in PHP?

In PHP you can create objects w/o calling the constructor. But that does not work by using new but by un-serializing an object instance. The constructor can then be called manually.

Can a class be executed without creating an object?

No. You need to create an instance of the class in order that the constructor runs.

How do you call a class constructor in PHP?

To call the constructor of the parent class from the constructor of the child class, you use the parent::__construct(arguments) syntax. The syntax for calling the parent constructor is the same as a regular method.


3 Answers

Unfortunately PHP doesn't have support to do this, but you are a creative and look guy :D

You can use an "factory", sample:

<?php

class Foo
{
   private $__aaa = null;

   public function __construct($aaa)
   {
      $this->__aaa = $aaa;
   }

   public static function factory($aaa)
   {
      return new Foo($aaa);
   }

   public function doX()
   {
      return $this->__aaa * 2;
   }
}

Foo::factory(10)->doX();   // outputs 20
like image 173
Felipe Cardoso Martins Avatar answered Nov 15 '22 20:11

Felipe Cardoso Martins


For this you can do a

https://www.php.net/manual/en/reflectionclass.newinstancewithoutconstructor.php

reflect your class and trigger the new instance without constructor.

Here a sample code:

<?php

class Test {
    private $end="";

    function __construct($value) {
        $this->end=$value;
    }

    public function alert($value) {
        echo $value." ".$this->end;
    }

    public function end($value) {

        $this->end = $value;        

        return $this; // return Test object so that you can chain to other function method.
    }
}

// Solution #1:
// reflect your class.
$reflector = new \ReflectionClass('Test');

// Then create a new instance without Constructor.
// This will ignore the constructor BUT it will create a new instance of class Test.
$say = $reflector->newInstanceWithoutConstructor();

// use end method that will return the Test object, then you can chain the alert()
$say->end('World!')->alert("Hello"); // output: Hello World!

?>
like image 26
Rey Mark A. Divino Avatar answered Nov 15 '22 20:11

Rey Mark A. Divino


Just do this (in PHP >= 5.4):

$t = (new Test("Hello"))->foo("world");
like image 30
Andrew U Avatar answered Nov 15 '22 20:11

Andrew U