I am new to OOP and therefore would like some help in designing a PHP class and subclass with any interface, if needed and so on. My base class is of type Policy with certain properties and there are specific subclasses of the Policy class with additional properties. My code:
<?php
/* Utility for calculating policy value, getting data from a CSV file
* and then generating output in an XML file.
* First we create the class Policy with all the below properties */
// base class with member properties and method
class Policy{
$policy_number;
$start_date;
$membership;
//Method which calculates the policy value depending on the policy type.
protected function calcPolValue() {
//Creates new xml format
$xml = new SimpleXMLElement('<xml/>');
//Ref: http://php.net/manual/en/function.fgetcsv.php
if (($handle = fopen("MyFile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$policy_number = $data[0];
$start_date = $data[1];
$membership = $data[2];
//checks whether $property1 string contains letter A
if ((strpos($policy_number, 'A') !== false) and ($start_date < 1990)){
//Policy is PolicyTypeA and its code here
}
elseif ((strpos($policy_number, 'B') !== false) and (strpos($membership, 'Y') !== false)){
//Policy is PolicyTypeB and its code here
}
}
fclose($handle);
}
//add the header and save the xml file in the root folder
Header('Content-type: text/xml');
$xml->saveXML("Policy Values.xml");
}
}
//can I write the subclass like this with the additional properties?
class PolicyTypeA extends Policy {
$management_fee;
}
class PolicyTypeB extends Policy {
$management_fee;
}
?>
How to go on designing the subclass and the method call in OOP? Hope my question is clear.