Reputation: 2800
I'm writing a bunch of classes in PHP for the server-side portion of a website we're developing. The classes look something like this:
class SomeEntity {
// These fields are often different in different classes
private $field1 = 0, $field2 = 0, ... ;
// All of the classes have one of these
static function create($field1, $field2) {
// Do database stuff in here...
}
// All of the classes have similar constructors too
function __construct($id_number) {
// Do more database stuff in here...
}
// Various functions specific to this class
// Some functions in common with other classes
}
The issue is there are a lot of these classes and they all need to have similar constructors and a few common functions, so I'd ideally want to write a superclass to handle all this stuff so that there's minimal copying/pasting going on. However, each of the subclasses has different instance variables and parameters, so what would the best way to design the superclass be?
(To phrase it perhaps slightly better, how can write a constructor function or other functions that do stuff with the instance variables of the class but without necessarily knowing what the class' instance variables are and hard-coding them by name?)
Upvotes: 2
Views: 991
Reputation:
Basically, you separate anything you repeat into either a parent class, or a helper class.
If it's a common activity that relates to the object, and would apply to similar objects, you put that in a parent class and inherit from it. If the children of this parent have similar members/properties but are named differently for whatever reason, you just write the method to accept parameters then pass the different property names in the call to that method.
If it's a common activity that relates to the object, and only that object, it becomes a method in the child class which needs it.
If it's a common activity that doesn't relate to the class in question, then you create a new class that manages things relating to that activity, and write a public method in that class that your other classes can call.
Upvotes: 0
Reputation: 62894
You can go quite a ways towards a very generic "Entity" type class, especially is you leverage the various magic methods.
Consider class like this (just some random convenience methods for entity-like classes to share):
<?php
abstract class AbstractEntity {
protected $properties;
public function setData($data){
foreach($this->properties as $p){
if (isset($data[$p])) $this->$p = $data[$p];
}
}
public function toArray(){
$array = array();
foreach($this->properties as $p){
$array[$p] = $this->$p;
//some types of properties might get special handling
if ($p instanceof DateTime){
$array[$p] = $this->$p->format('Y-m-d H:i:s');
}
}
}
public function __set($pname,$pvalue){
if (! in_array($pname,$this->properties)){
throw new Exception("'$pname' is not a valid property!");
}
$this->$pname = $pvalue;
}
}
<?php
class Person extends AbstractEntity {
protected $properties = array('firstname','lastname','email','created','modified');
}
Upvotes: 5