Home php Creating objects "on the fly"

Creating objects “on the fly”

Author

Date

Category

All good day!

Previously, when creating a class object, I somehow did not think about convenient object creation and use of its useful functionality “on the fly” (ie in line). Probably the lion’s share of the object-oriented languages ​​support this feature. How do things stand with the same in PHP?

Normally used useful functional class by first creating an object and then calling its methods / use class variables:

$ OBJECT = new Object ();
$ OBJECT - & gt; CallUsefulMethod ();

But in many cases it is known that the object will be used only once, therefore, in assigning him a liter. there is no point variable. However, this is the beauty of creation “on the fly”. Is there a way in PHP to do something like this (?):

(new Object ()) - & gt; CallUsefulMethod ();

Thank you;)


Answer 1, Authority 100%

@ 0xFFh else you can write a method create and enjoy it instead new:

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

Technically, almost the same, but it is currently obyaekty are based on input parameters.

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

And if the method returns $ this – and you can chain a la jQuery build

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

A small listing of comment code

& lt;? 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- & gt; {$ 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- & gt; meow. '& Lt; br / & gt;';
 }
}
$ Cat = Cat :: create ()
 - & gt; set ( 'color', 'white')
 - & gt; set ( 'height', 10)
 - & gt; execute ( 'sayMeow')
 - & gt; set ( 'meow', 'Myaaaw')
 - & gt; execute ( 'sayMeow');
? & gt;
out:
 MEOW.
 Myaaaw

Answer 2, authority 86%

Unfortunately, PHP does not support anonymous objects.
You can use the pattern “Singleton”.

& lt;? 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 () - & gt; SayMeow (); // using
? & gt;

Answer 3, authority 14%

It is possible (try on php7):

& lt;? Php
class A
{
  public function sayB ()
  {
   echo 'I say B';
  }
}
(New A ()) - & gt; sayB ();

It is necessary to think about the expediency – because the object will be created in memory but it will not be links. After using the right method, you can not remove the object, if necessary. It will be deleted from the memory only when zavreshenie work your entire script.


Answer 4

& lt;? Php 
$ OBJ = new stdclass ();
   $ Obj- & gt; Property1 = 'String';
   $ OBJ- & GT; Property2 = 13;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions