Can PHP objects be built and their variables set in one operation?

In perl, I'm used to doing

my $foo = new WhatEver( bar => 'baz' ); 

and now I'm trying to figure out if PHP objects can be created this way. I see only this:

 my $foo = new WhatEver(); $foo->{bar} = 'baz'; 

Is it possible to do this in one step?

+7
source share
4 answers

You can lay out your constructor as follows:

  class MyClass { public function __construct($obj=null) { if ($obj && $obj instanceof Traversable || is_array($obj)) { foreach ($obj as $k => $v) { if (property_exists($this,$k)) { $this->{$k} = $v; } } } } } 

This has a number of disadvantages:

  • It is ineffective
  • The variables you created will not be displayed on any software that you use
  • This is an open door for all forms of dementia.

However, it also provides the following benefits:

  • This can be done fairly safely.
  • This allows lazy variables to be executed.
  • It also allows you to set private variables as long as you know their names. This is very good in this regard, if not abused.
+3
source

The parameters passed in parentheses (which can be omitted, by the way, if they are absent) go to the constructor method, where you can do whatever you want. If the class is defined, for example, as follows:

 class WhatEver { public $bar; public function __construct($bar) { $this -> bar = $bar; } } 

Then you can give it all the necessary values.

 $foo = new WhatEver('baz'); 
+1
source

There are several ways to do this, but each has its own drawbacks.

If your setters return an instance of the object itself, you can bind your methods.

 my $foo = new WhatEver(); $foo->setBar("value")->setBar2("value2"); class WhatEver { public $bar; public $bar2; public function setBar($bar) { $this->bar = $bar; return $this; } public function setBar2($bar2) { $this->bar2 = $bar2; return $this; } } 

However, this does not reduce it to one step, but simply condenses each step after creating the instance.

See: a chain of PHP methods?

You can also declare your properties in your constructor and simply pass them for installation at creation.

 my $foo = new WhatEver($bar1, $bar2, $bar3); 

This, however, has the disadvantage that it is not openly extensible. After several parameters, it becomes unmanageable.

A more concise but less efficient way would be to pass a single argument, which is an associative array, and iterate over it by setting each property.

+1
source

The implicit assumption here is that objects have significant, supposedly public, properties that provide values ​​for the calling code. This is by no means predetermined β€” encapsulation is a key aspect of OOP, so the primary access to an object is through its methods.

The "correct" mechanism for initializing the state of an object is its constructor, and not a series of property assignments. What arguments the constructor takes are consistent with the class definition.

Now the constructor may have a long series of named parameters, so you could write $foo = new WhatEver(1, "hello", false, null) , but if you want them to act as parameters, then it could take one hash - in PHP terms, an array as its argument.

So, to answer the question, yes, if your constructor has the form function __construct(Array $options) , and then iterates over or checks for $options . But it depends on the constructor what to do with these options; for example, passing [ 'use_safe_options' => true ] can cause the entire set of private variables to be set to documented β€œsafe” values.

Starting with PHP 5.4 (which introduced [ ... ] as an alternative to array( ... ) ), it takes only a few character strokes than the Perl version:

 $foo = new WhatEver( ['bar' => 'baz'] ); 
+1
source

All Articles