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.