The difference between the parameters that belong to the object

This is a rather specific problem that I cannot find anywhere else. This applies to many languages, but I especially want to understand PHP. For the sake of it I will use "events" as our classes. The easiest are two types:

abstract class Event{ protected $cost; protected $startTime; public function __construct(){ foreach($eventData as $key => $val){ if(property_exists($this, $key)){ $this->$key = $val; } } } } 

and

 class Party extends Event{ private $hasPointyHats; public function __construct($eventData){ parent::__construct($eventData); $this->hasPointyHats = $eventData->hasPointyHats; } } 

A class event has 2 details: cost and start time. When it is constructed, it should be passed an associative array containing all the parameters of the event, and it will automatically set the property to the value of the passed array.

Class Party extends the event by establishing whether this event will have these amazing spiky hats (I won’t leave if it’s not).

And no. No no. Because when you pass this object:

 //Some result set... $mysqli_result = $result->fetch_assoc(); 

which has a cost, startTime and hasPointyHats (maybe even more!), you get the following small error:

 Fatal error: Cannot access private property Party::$hasPointyHats in C:\somePath\Event.php on line 35 

I understand why. Because in the "Event", $ it refers to the "party" object, but it is private. I do not want to rewrite each property of the object in the super constructor, only those that belong to the superclass (the abstract class Event). Is there a way to target specific properties of the Event class rather than child classes? Thus, no matter what object I am producing it, I will not accidentally set the properties in the child class, because the transferred object had some kind of conflicting property?

I assume this is a bit dumb thing, such as a super-> property or something else, but I still need to use the exists property with it.

Thanks for the help in advance.

+4
source share
2 answers

You can use property_exists only with the class name instead of the object. Like this:

 abstract class Event{ protected $cost; protected $startTime; public function __construct($eventData){ foreach($eventData as $key => $val){ if(property_exists(__CLASS__, $key)){ $this->$key = $val; } } } } 
+2
source

property_exists() specifically points out in the documentation that with PHP 5.3 it checks for the existence of a property regardless of availability , which explains the behavior you see.

get_object_vars() , on the other hand, gets a list of specific properties in the area , so work better for your purposes.

Try something like this:

 public function __construct() { $props = get_object_vars($this); foreach ($eventData as $key => $val) { if (array_key_exists($key, $props)) { $this->$key = $val; } } } 

This does not mean that this is a property of the parent class, just accessible from the parent class.

If you really want to restrict only the parent class, get_class_vars() might be even better:

 public function __construct() { $props = get_class_vars(__CLASS__); foreach ($eventData as $key => $val) { if (array_key_exists($key, $props)) { $this->$key = $val; } } } 
+1
source

Source: https://habr.com/ru/post/1487206/


All Articles