I'm new to OOP and working on a local directory site where you can search local shops and companies. I have to make some classes like string_class
(which will deal with strings) ,city_class
(which will deal with city),state_class
(which will deal with state),category_class
(which will deal with company category) and company_class
(which will deal with company). Now I'm confused about where to start and where to end for using a real concept of OOP.
Let's think that I have to make these five classes
<?php
class string_class(){
}
class city_class(){
}
class state_class(){
}
class category_class(){
}
class company_class(){
} `
?>
Now do you think should i extend string class for making city class ?? like
class city_class extends string_class{
// DO SOME CITY STUFF HERE
}
and then
class state_class extends city_class{
// DO SOME STATE STUFF HERE
}
and then
class company_class extends state_class{
// DO SOME COMPANY STUFF HERE
}
[...] about where to start and where to end for using a real concept of OOP
You need to identify the invariants of your class; that is, conditions that must always be true for instances of the class. In your example, you want to prevent creation of city_class
objects that do not have correct city names. So--
class city_class {
function __construct($name) {
if (!$this->validate_city($name))
throw new city_exception();
}
}
Next, you need to list the states that are valid for objects of your city_class
. For example, those might be "new", "saved", "updated", and "deleted".
For your objects to transition from one state to another, a certain event (or trigger) must happen. For example, when your object is first created, its state is new; when your object is saved to database, its state becomes "saved"; when the properties of the object are changed, its state becomes "updated"; and so on.
Notice that each transition requires an action to get into the next state; for example, the action of creation is required to reach the "new" state; the action of saving is required to reach the "saved" state; and so on. Therefore, you can make methods for your class out of those actions.
class city_class {
var $saved;
var $name;
function __construct($name) {
if (!$this->validate_city($name))
throw new city_exception();
$this->name = $name;
}
function save() {
$this->saved = true;
}
function update($new_name) {
if (!$this->validate_city($new_name))
throw new city_exception();
$this->name = $new_name;
$this->saved = false; // updated state
}
}
In your comment, you list a responsibility of city_class
as getting "id and name" from database--this is not proper OO design.
Your city_class
knows only about itself and its collaborators at the same level of abstraction. Databases are at a different level of abstraction, and your city_class
must not know about them. Therefore, for database access, you would need a city_db
class that would be responsible for retrieving data and building city_class
objects. Again, you follow the same process of identifying invariants and states of city_db
, and making methods out of the events that cause transitions from one state to another.