Page 1 of 1
Implementing class
Posted: Sat Jul 28, 2007 12:25 pm
by shivam0101
Hello,
Should i have to call only the methods or should i have to define the function?
For example, if i want to insert name, should i have to write,
Code: Select all
class homePage extends mainClass
{
function InsertName($name)
{
$this->insert($name, 'name', 'register');//value, fieldname, tablename
}
}
OR
Should i have to write this method in mainClass and call it like,
Code: Select all
$class_obj=new mainClass;
$class_obj->InsertName($name);
Thanks
Re: Implementing class
Posted: Sat Jul 28, 2007 12:35 pm
by superdezign
It depends on which class should have the responsibility of inserting the name.
Posted: Sat Jul 28, 2007 1:47 pm
by shivam0101
thanks for replying.
What i meant to ask is, should i have to write classes in every page or should i have to call the methods defined in a class (all methods defined in a separate class).
Posted: Sun Jul 29, 2007 7:56 am
by superdezign
shivam0101 wrote:thanks for replying.
What i meant to ask is, should i have to write classes in every page or should i have to call the methods defined in a class (all methods defined in a separate class).
Sorry, your re-wording is no clearer than your original post.
If I'm understanding you correctly, then please keep in mind that it is common practice to separate all classes into their own files, then include those files when you need them.
Posted: Sun Jul 29, 2007 8:02 am
by s.dot
Every method should not have it's own class -- if that's what you're asking. Technically, it could. But usually a group of methods are best suited to perform specific tasks -- and it's okay to have that group of methods together in the same class.
Posted: Mon Jul 30, 2007 12:09 am
by shivam0101
superdezign,
My question is,
Code: Select all
$choice=$_POST['ch'];
class homePage extends mainClass
{
function InsertName($name)
{
$this->insert($name, 'name', 'register');//value, fieldname, tablename
}
function DeleteName($name)
{
$this->delete($name, 'name', 'register');
}
}
$obj=new homePage;
if($choice=='insert');
$obj->InsertName('myname');
elseif($choice=='delete')
$obj->DeleteName('myname');
is correct?
OR
Code: Select all
$choice=$_POST['ch'];
$main_cls=new mainClass;
if($choice=='insert');
$main_cls->InsertName('myname');//insertName is defined in mainClass
if($choice=='delete');
$main_cls->DeleteName('myname'); // DeleteName is defined in mainClass
is correct?
i.e should i have start each page with a class and define relevent methods and call them Or i should define all methods in a seperate Class called(mainClass) and only call them?
Posted: Mon Jul 30, 2007 7:10 am
by superdezign
Neither is "correct."
As for the first example, you shouldn't need to create a separate class for every page. Maybe different actions that are taken by the different pages, but the pages themselves shouldn't need their own classes.
As for the second, if that action is not supposed to be handled by mainClass, then don't make it an action of mainClass. If it is, then your second example is fine.
Posted: Mon Jul 30, 2007 8:28 am
by shivam0101
Neither is "correct."
can u tell me the right way with that same code?
Posted: Mon Jul 30, 2007 8:41 am
by superdezign
shivam0101 wrote:Neither is "correct."
can u tell me the right way with that same code?
No. It's your application, and I have no idea what it's doing.
Posted: Mon Jul 30, 2007 9:16 am
by Chris Corbyn
superdezign wrote:As for the first example, you shouldn't need to create a separate class for every page. Maybe different actions that are taken by the different pages, but the pages themselves shouldn't need their own classes.
Not exactly

MVC is a typical design pattern where each page will have at least one class which runs it (the controller).
Code: Select all
class HomeActions extends Actions {
}
class DocumentationActions extends Actions {
}
You may even have a separate class for each action. It looked from the first post that this may be what's going on.
shivam0101, unless the values you are setting are concrete values (i.e. they'll always exist and you know what their names are) then it's usually better to be more abstract in my opinion. If you create a general method which takes a key and a value there's no restriction on how many different keys can be set. With concrete methods you lose that flexibility but you gain some clarity when people read your code and especially when you run a documentation tool like PHPDocumentor over it.
In PHP 5, there is actually a special method named __call() which could give you the best of both worlds with the huge drawback that you lose clarity in your code and can potentially have bugs creep in un-noticed.