I am starting to learn PHP and have a decent Java background. I came upon the following coding challenge in the PHP langauge:
I want to be able to encode/decode my PHP classes in JSON format. I have created PHP classes that utilize getter/setter methods for the classes private properties. This caused the native json_encode method to skip skip these properties during encoding. I found the following script which picks up private properties and formats them in a JSON string:
<?php
public function encodeJSON()
{
foreach ($this as $key => $value)
{
$json->$key = $value;
}
return json_encode($json);
}
public function decodeJSON($json_str)
{
$json = json_decode($json_str, 1);
foreach ($json as $key => $value)
{
$this->$key = $value;
}
}
?>
I studied up on this code and realized how it was generating the strings and objects. Now I would like to push this code into an abstract class, so that I may extend that class and receive this functionality.
The problem is I cannot reference the private properties in the concrete class from the abstract class. My knowledge of Java tells me that I may not be able to do this without specifying an abstract method and forcing each concrete class to implement it. I tried several attempts at using reflection in PHP and passing the concrete class into the constructor of the abstract class. Can anyone point me in the right direction or just tell me if this isn't possible?
protected? (The concept of private variables is not to be able to see them in delivered classes so it's correct behavior)