I like to ask a question related to php classes! Let's assume I have a clients' table, a streets table and another table called towns. In OOPs I must assume that I have a clients class, a streets class and also a towns class. So every class is related to another one like the following:
Client: clientId, clientFirstname1, clientLastname1, clientAddress, clientStreetCode, clientTownCode, clientPostCode.....
Streets: streetCode, streetName, townCode
Towns: townCode, townName
What I would like to ask is, if I am creating a client record shall I extend one class to another or shall I leave them NOT EXTENDED, and then include them in php as single classes?
Extending in OOP isn an is-a
relation. In terms of the three classes Client
, City
, Street
, none of them is-a
instance of any other of them.
Instead you should just use references. A Client
has a Street
and a Street
has a City
.
class Client {
/**
* @var Street
*/
protected $street;
// ....
}
class Street {
/**
* @var City
*/
protected $city;
// ...
}
class City {
// ...
}
Maybe you should also think about introducing a new class Address
, which contains an reference to a City
and a Street
, instead of an Street
with an reference to an City