Implementing class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Implementing class

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Implementing class

Post by superdezign »

It depends on which class should have the responsibility of inserting the name.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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).
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Post by shivam0101 »

Neither is "correct."
:(

can u tell me the right way with that same code?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply