2

I am developing an application in which I have a Class containing various methods. The problem is that i need to call most of the methods on form submission. For that reason, I have to create a new file on each form submission. On every file, I have to create an instance of the same class and then call the appropriate method.

Is there any better way to call methods in such situation so that I don't have to create multiple files having similar functionality in php? I surely don't want to use any framework.

3 Answers 3

1

You dont need to create a separate PHP file for each form submission.

<form method="post" action="myapp.php">
 <input type="hidden" name="action" value="useraccount">
 ...
</form>

On the PHP side of things, just check the posted 'action' variable, and respond accordingly.

Actually, what you want to do is to have a master 'view' controller that handles the interface, and load templates of the pages you need through that. This way you dont have a single php page per web 'page' that the user sees - just the one. Then you're not creating massive amounts of PHP files, and not duplicating all the include()'s on every page. If you then have a need to make it look like separate pages for some reason, you use mod_rewrite.

3

Maybe this is a good point to start applying some design patterns and good old OOP princiles.

AFAIK having many files isn't bad as long as the application is structured correctly.

If you follow the SOLID principles of OOP you will end with a loosely coupled and well structured application.

Actually following the first principle (Single responsibility principle) will result in more classes (and files eventually), but ur code will be more cohesive.

-1

Can't you use the include function? Make your file with the Class once, include it in projects that use the methods.

include 'vars.php';
3
  • I am using includes in fact, but still i have to create a new file for each form submission. Because I am calling these methods through the "action" attribute of <form> tag. for example, <form action = "method1.php" > <form action = "method2.php">
    – varun
    Commented May 16, 2012 at 11:02
  • form action = "methods.php&method=1" form action = "methods.php&method=2"
    – Pieter B
    Commented May 16, 2012 at 11:09
  • It seems to be a good solution. thanks. m going to implement this. I will mark this solved after implementation
    – varun
    Commented May 16, 2012 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.