PHP 5 Objects, Patterns, and Practice

Category: Programming
Author: Matt Zandstra
4.5
All Stack Overflow 8
This Month Stack Overflow 3

Comments

by anonymous   2019-07-21

If you are using Kohana, you should use modules for stuff you don't want to duplicate for every application. Then you can keep specifics in your application with extended classes or specific settings in config files.

http://kohanaframework.org/3.2/guide/kohana/modules

A lot of if statements are mostly an indication that you need to do refactoring. A large cascading if statement to allow for different sites to be loaded is bad practice in the sense that you make files tightly coupled causing to edit multiple files when you need to make a simple addition or change. Furthermore, it will eventually become ugly if every site needs to load different dependencies or settings or whatever in your if statement.

It's difficult to say what you need to change without seeing the code, but try looking at design patterns like the factory or abstract factory design patterns to create site objects.

A good book that deals with the subject of patterns and best practices with PHP is PHP 5: Objects, Patterns and Practice by Matt Zandstra: http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804. A very good book.

by anonymous   2019-07-21

In your case I would make the switch to Object Oriented Programming. I've been in a similar situation like you but wasn't satisfied anymore at how my websites looked with simple procedural scripting. Switching to OOP will take quite some time to get used to, but in my opinion it was all well worth the effort.

Take your time learning OOP. First, check http://www.php.net/ to learn about objects and classes and afterwards read a book on the subject (Php Objects, Patterns And Practice by Matt Zandstra is a very good one http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804). You will find out why a lot of people have made a switch to the OOP approach for web apps.

When you are used to the way of thinking in OOP, choose a framework. It doesn't make sense to write code for things that have been written 100 times before and are tried and tested approaches to common problems. A framework covers all the basic annoying stuff you shouldn't be spending your precious time on. But only choose a framework when you have really grasped the way of thinking with OOP. It's always best to learn the basics first instead of jumping right in the middle by directly choosing a framework.

by anonymous   2017-08-20

You only have one thing right with OO. The $connection object.

Everything else is procedural. You should start by creating a class, instead of all those loose functions in "dbfunctions.php".

Also note that you should avoid mixing logic code with HTML. It gets hard to maintain.

Here's probably the best book you can read on the subject .

It's not easy to get into object oriented paradigm. But when you get it, it's like riding a bicycle. You never forget.

by anonymous   2017-08-20

You might find investigating design patterns helpful. The composite pattern is often used in this situation. A good book to help you with design patterns with php oo is Matt Zandstra's PHP Objects, Patterns and Practice

by anonymous   2017-08-20

My favourite book on OOP for PHP, very good!

http://www.amazon.co.uk/PHP-5-Objects-Patterns-Practice/dp/1590593804