the question is simple. I have a base abstract class (person). From this i have extended another class (patients).
I save personal information (e.g. firstname, lastname) in PERSONS table [there is an ADD function. ]. Patient specific information (like illness, medicines, …) are saved into a separate PATIENTS class. [there is an ADD function that calls the parent, then some code].
How do I prevent person’s add function from accessing properties defined inside it’s child, patients?
In order to make it more clear, here is a sample code:
class P {
public P_var = 'Anoush' ;
public function add ()
{
// find all properties:
foreach ($this as $prop => $val )
{
$insertables [$prop] = $val ;
}
// insert all VALUES FIELDSET etc. based on the array created above
}
class CH extends P {
public CH_var1 = 'ravan' ;
public CH_var2 = 'something' ;
}
Then when I call add, the $insertables will contain P_var, CH_var1 , CH_var2. I only want it to have P_var.
Thanks
You can do this by using Reflection (see http://www.php.net/manual/en/book.reflection.php).
However, I recommend refactoring your code instead to get a cleaner solution (e.g. add a method
getProperties()(maybe defined by an interface) or whatever. Then let your database class invoke that function in order to get the list of properties to store in the database.