What is wrong with your PHP-code?
The PHP blog touts more often the examples of bad code and anti-patterns. Well, someone else is criticizing the Hindus for the code ...
The code quality is something that should not be ignored or put aside for later. Such a delay is technical debt or code debt that will backfire for sure. You have to spend more time creating quality code and application architecture.
It is highly recommend to read Uncle Bob's principles of SOLID: butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod.
This text can greatly improve your ability to build scalable systems, particularly the principle of SRP.
Global variables are the worst that could be in your code. The code with global variables cannot be moved anywhere (because there may not be required variables). Such a code freezes the code around itself (namely when it is changed the global variables could be affected), this code becomes very delicate, and it may stop working under any changes. Also, it has a lot of subtle bugs that is not the best additive from the global variables.
Here are more details: SO, Gordon, Robert Martin.
Public class properties
Classes can define special variables called properties. A property, also known as a member variable, holds data that can vary from object to object.
A property in a class looks similar to a standard variable except that you must precede your declaration and assignment with a visibility keyword. This can be public, protected, or private, and it determines the scope from which the property can be accessed.
Static methods and singletons
Singleton is an anti-pattern. There are no good singletons. Joining with a database is bad case too for the singleton. Singleton is unified for the entire code, and actually it is a property of global variables with all the consequences.
The classes with static methods are bad for the fact that the code is hard to test, because the class names are hardcoded and cannot be replaced by any objects. Class name hardcoding makes the system brittle and reduces the possibility of code reuse.
Here are good and interesting articles, such as: “Singletons are pathological liars”, “Static methods are death to testability” and “Writing Testable Code” written by Miško Hevery.
Arrays as a set of parameters for the function
$result = some_function(array(
'connection' => $connection,
'driver' => $PDO,
'user' => array(
'login' => 'admin',
'password' => $password
This is very bad, because every time you write a function call, you will need to refresh a memory, particularly what is the array format, and what are the keys. Any of your code will seem absolutely strange after a while. This is only a matter of time itself. Moreover, if you want to change these parameters and make new ones in order to keep everything working, you will think of this argument passing method as a horrible dream.
In the arrays cannot be set the required and optional parameters, default values, type-hinting and IDE. All this is possible with usage of conventional arguments. If there are many parameters, they can be set in the setters ($object-> setConnection($ PDO)).
Use the namespaces
The biggest plus of namespaces is autoloading. You will not need to write dozens require and include, spread throughout the code. All classes will be loaded automatically.
Each class and each interface should be created in a separate file. The APC assures reading from the disk.
You have to follow PSR-0 for the creation of namespaces. It is pretty simple and allows one autoloader to download all modern PHP components (and PEAR). Getting rid of require and include gives a feeling of comfort and order.
Constantly improve your skills
Sometimes a programmer gets an idea that knows everything and it is meaningless to spend the time to read anything else. It is called the Dunning-Kruger effect. This is a very bad idea, because it leads to stagnation of the professional level. The code is useless if it was written in such period, it is easier to write from scratch than to fix that.
Reading a book may take a day or even three. However, a good book can save at least one year of your development. Do not you want to lose a few years for nothing? Allow yourself a few days for reading and it will pay for itself.
Clean code. The creating, analysis and refactoring, Robert Martin, ISBN 978-5-459-00858-6, 978-0132350884.
It is very simple and useful book.
Patterns of Enterprise Application Architecture, Martin Fowler, ISBN 978-5-8459-1611-2, 0-321-12742-0.
The book is more useful to the professionals (it will be difficult for beginners to understand.) Many terms of this book can be founded at martinfowler.com website.
Code Complete, S. McConnell, ISBN 978-5-7502-0064-1, 0-7356-1967-8.
This will give something to argue with each other and professionals.
Continuous communication with clever people is very useful for the development and the level of programming.
Here is a link to StackOverflow, where you can ask questions or chat with the PHP developers.
The people’s ability to share knowledge helped human civilization. Take advantage of this ability. Have a nice refactoring :)
UPD: The main point of this article to provide references to the books, which will be very useful and give information about the pitfalls of anti-patterns and other useful things.
|Vote for this post
Bring it to the Main Page