Page 1 of 1

PHP Class - Best Practices

Posted: Mon Feb 08, 2016 7:29 am
by A31
Hi,

I am fairly new to classes & OOP in general and was wondering, what is the official best practices for classes with regards to what goes into a class.

By this I mean: Should a HTML form be inside a class?

What should be in a class and what should actually just be done normally?

Thank you so much for your help!

Kind Regards

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 7:59 am
by Celauran
A31 wrote:Should a HTML form be inside a class?
It depends. Should you have a class method that echoes a form? No, probably not. A form builder class? Sure, that could work.
https://laravel.com/api/5.0/Illuminate/ ... ilder.html
http://api.symfony.com/2.8/Symfony/Comp ... ilder.html
A31 wrote:What should be in a class and what should actually just be done normally?
I'd lean toward wrapping as much in classes as you can. Helps keep your code clean, improves readability, promotes code reuse, and limits side effects.

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 8:09 am
by A31
Thank you very much!

Currently how I do it is that I create a method for the form and then a method for the processing all inside the same class.

The problem comes that I use jQuery & AJAX mostly to submit the form. So where the problem then comes in is that I first have to write a normal function activating the class, because I cant call a method from a class directly out of jQuery.

So the steps are:
1. Initialize the Class
2. Display the form Method
3. jQuery Ajax Validates & Posts to normal function
4. The function then Initialize a new instance of the class and runs the Process Method
5. The result is then returned to the function
6. The function then returns the result to jQuery which then notifies the user.

So I was wondering if my form & process is a once off / rather unique process, would it not be better to skip the class and just use the function? It will basically skip 2 steps.
I then would just use classes to do repeatable actions that I will use in multiple places in my app.

In saying all of this I would like to stick with what is the best practices though even if it means adding 2 steps for every process.

I hope this all makes sense?

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 8:24 am
by Celauran
A method that displays the form (ie. echo '<form...>' wrapped in a function tag) is brittle and precisely the sort of thing I was advising against. What I would do in the scenario you describe is expose an endpoint that your JavaScript can call, have my dispatcher / controller resolver instantiate the controller, and either handle validation directly in the controller method, or hand it off to a dedicated validator class, depending on the size and complexity of the form.

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 8:51 am
by A31
Great that makes sense! Thank you very much.

Then last question for this post, where can I read more about the best practices etc of classes & the way to work with them?
There are many many tutorials on how to make a class etc but I find few that explains the proper ideology or the workflows.

Thanks again for your feedback!

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 9:20 am
by Celauran
Far too many outdated tutorials out there, and not a lot that deals with practical, real world applications of OOP. That said, PHP: The Right Way is probably the best resource I know of. Josh Lockhart's Modern PHP is also excellent.

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 5:10 pm
by Christopher
A31 wrote:Then last question for this post, where can I read more about the best practices etc of classes & the way to work with them?
There are many many tutorials on how to make a class etc but I find few that explains the proper ideology or the workflows.
An important thing for you to do when expanding your toolbox of software development tools is to understand the WHY of things like OOP. OOP adds a set of new features to languages (i.e., classes, methods, properties, etc.) and ways to use those language features. These things were not just thought up, they are the result of trial and error development of solutions to real-world problems. The features we have today proved to be superior solutions.

With OOP specifically, the core features provide the ability to implement Encapsulation, Abstraction, Polymorphism and Inheritance/Composition. You can do more research on these, or more importantly how different OOP patterns use these features to solve real-world problems. The world of OOP and Design Patterns is interesting because it actually publishes the design of Best Practice solutions to for many, many software problems.

Re: PHP Class - Best Practices

Posted: Mon Feb 08, 2016 11:44 pm
by A31
Thank you very much for the insights. I appreciate it alot!