Good day to all!

Earlier, when creating an object of a class, I somehow did not think about the convenient creation of an object and the use of its useful functionality "on the fly" (ie, in line). Probably, the lion's share of OO languages ​​supports this feature. How is the same with PHP?

Usually, useful class functionality is used by first creating an object and then calling its methods / using class variables:

$OBJECT = new Object(); $OBJECT -> CallUsefulMethod(); 

But in many cases it is precisely known that the object will be used once, therefore, in assigning it to a. there is no point in a variable. However, this is the beauty of creation "on the fly." Are there ways to do something like this in PHP (?):

 (new Object()) -> CallUsefulMethod(); 

Thank ;)

  • Sorry, but I still can not refrain from a counter question. And why is it actually? If you write a class to use its only method and immediately forget about it, you simply don’t need a class, you need a function. You should not fence OOP for OOP. - Pavel Vershinin
  • No, the class encapsulates a large logically unified structure, the elements of which actively interact with each other. But also, sometimes, it is necessary to get some data from the class, which, in fact, I would like to receive on the fly, without auxiliary variables. - AseN
  • I think you can use static methods, they can be used in general without declaring the class php.net/manual/ru/language.oop5.static.php - Pavel Vershinin
  • I do not need to explain what statics is in OOP :) It will not work, since the data is generated in accordance with the input parameters, which, in turn, are different. So, as I understand it, you cannot create objects on the fly in PHP? :( - AseN
  • one
    And why can't we just create an instance of a class and keep somewhere a link to it and call it as necessary? Creating objects in this way in PHP is an expensive operation. - zhenyab

4 answers 4

@ 0xFFh you can also write the create method and use it instead of new:

 public static function create($args) { $obj = new self; // some code return $obj; } 

Technically, almost the same thing, but the objects are quite self-created taking into account the input parameters.

 $catsMeow = Cat::create($color, $height, $length)->SayMeow(); 

And if the methods return $this - you can also create chains for jQuery

 $cat = Cat::create('white', 10, 30)->feed($meat)->colorize('black'); 

Small listing code by comment

 <?php class Cat { private $color = 'red'; private $height = 15; private $length = 45; private $meow = 'Meow'; function create() { $obj = new self; return $obj; } function set($property, $value) { $this->{$property} = $value; return $this; } function execute($method, $args = array()) { $cb = array($this, $method); if (is_callable($cb)) call_user_func_array($cb, $args); return $this; } function sayMeow() { echo $this->meow . '<br />'; } } $cat = Cat::create() ->set('color', 'white') ->set('height', 10) ->execute('sayMeow') ->set('meow', 'Myaaaw') ->execute('sayMeow'); ?> out: Meow Myaaaw 
  • one
    @ 0xFFh redid :) Then I will also recommend the method ->set($property, $value) , returning $this , and execute , performing the method and also returning $this (regardless of the return value of the method). Php-shny code becomes unusual, but beautiful) Added listing in response - Sh4dow

Alas, PHP does not support anonymous objects. You can use the pattern "Singleton".

 <?php class Cat { protected static $instance; // object instance private function __construct() { ; } //constructor block private function __clone() { ; } //cloning block private function __wakeup() { ; } //unserialize block public static function getInstance() { if ( !isset(self::$instance) ) { $class = __CLASS__; self::$instance = new $class(); return self::$instance; } return self::$instance; } public function SayMeow() { echo("Meow!"); } } Cat::getInstance()->SayMeow(); //using ?> 
  • one
    Anonymous cat =) - uzumaxy
  • Yes thank you. The solution is all based on the same static encapsulated "no less static" pattern. So, in essence, nothing new. - AseN
 <?php $obj = new StdClass(); $obj->property1 = 'string'; $obj->property2 = 13; 

    It is possible (tested on php7):

     <?php class A { public function sayB() { echo 'I say B'; } } (new A())->sayB(); 

    Only it is worth thinking about expediency - the object will be created in memory but there will be no link to it. After using the desired method, you can not, if necessary, delete the object. It will be removed from memory only when the completion of the work of your entire script.

    • In addition to the above: in your example $ OBJECT = new Object (); the $ OBJECT variable is a pointer to an instance of the class (object), and not the variable in which it is contained. So that the memory does not "leak", you need to create an object classically using new, and then $ OBJECT = null - delete the pointer. The destructor will be "magically" called and will remove the object from memory. - kolobok
    • comment "catch up" transfer to the body of the answer if it applies to him. Click the "edit" button under the answer and make the appropriate changes. - Mikhail Rebrov
    • Such inline-s only appeared in version 7. I do not understand what you mean by "leaking" memory, if everything is under the control of gc? And the object will not be deleted after the completion of the script, but after returning from the method, in other words, going beyond its namespaceʻa - AseN