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!
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).
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.
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.
$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?
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.
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).
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.