Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a special object initializer construct in PHP like there is now in C#?

Tags:

I know that in C# you can nowadays do:

var a = new MyObject {     Property1 = 1,     Property2 = 2 }; 

Is there something like that in PHP too? Or should I just do it through a constructor or through multiple statements;

$a = new MyObject(1, 2);  $a = new MyObject(); $a->property1 = 1; $a->property2 = 2; 

If it is possible but everyone thinks it's a terrible idea, I would also like to know.

PS: the object is nothing more than a bunch of properties.

like image 795
Matthijs Wessels Avatar asked Oct 01 '10 09:10

Matthijs Wessels


People also ask

What is __ construct in PHP?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

How many types of constructors are there in PHP?

What are the Types of Constructor in PHP? In any object-oriented programming, three types of Constructor are mainly utilized.

What are the __ construct () and __ destruct () methods in a PHP class?

Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.

How do I declare an object in PHP?

To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.


2 Answers

As of PHP7, we have Anonymous Classes which would allow you to extend a class at runtime, including setting of additional properties:

$a = new class() extends MyObject {     public $property1 = 1;     public $property2 = 2; };  echo $a->property1; // prints 1 

Before PHP7, there is no such thing. If the idea is to instantiate the object with arbitrary properties, you can do

public function __construct(array $properties) {     foreach ($properties as $property => $value)      {         $this->$property = $value     } }  $foo = new Foo(array('prop1' => 1, 'prop2' => 2)); 

Add variations as you see fit. For instance, add checks to property_exists to only allow setting of defined members. I find throwing random properties at objects a design flaw.

If you do not need a specific class instance, but you just want a random object bag, you can also do

$a = (object) [     'property1' => 1,     'property2' => 2 ]; 

which would then give you an instance of StdClass and which you could access as

echo $a->property1; // prints 1 
like image 90
Gordon Avatar answered Sep 28 '22 03:09

Gordon


I suggest you use a constructor and set the variables you wish when initialising the object.

like image 21
Thariama Avatar answered Sep 28 '22 03:09

Thariama