PHP Tips and Tricks - Codular
codular.com
codular.com
But I really disagree with some of this.
First, why call variable references "pointers"? You're just breeding ignorance there. Understanding pointers is a valuable skill for a software engineer, even if they'll never use the skill directly in their day job as a PHP developer.
Second, you're introducing concepts that are a little sleazy like variable variables without any commentary on downsides or best practices. There is effectively nothing you can do with a variable variable that you cannot do with an associative array.
And fine, if you don't want to editorialize, if you want to just teach facts of the language without adding opinion, why invent the term "pointers" that, as far as I've ever read, is not used in any official PHP documentation.
${$var};
${$name.'Controller'}->function();
$something->{$functionName}();
With all the appropriate warnings, of course. For a start, don't find excuses to implement this style of code. You almost certainly don't need it for a basic website.With the autoloader, there is no need to do include_once(), simple include() is enough. The file will be loaded only once for every class.
I mean, is there anyone here who doesn't know what the ternary operator is?
(It's left associative, unlike literally every other language that has a ternary operator.)
Custom autoloading scheme instead of PSR-0? Classname.inc.php, seriously? Moving on, dynamic variables are a terrible unfeature that should be avoided. References usually cause more harm than good and should be avoided as well. And finally, a class which is named with a redundant "Class" suffix, containing only static members, aka class-oriented programming.
[edit] Or, time() for that matter, in fact this could be a subtle bug, as the return value of time() may vary throughout the function (unlikely, I know!)
spl_autoload_register(function ($class_name) {
require(dirname(dirname(__FILE__)) . "/classes/" . $class_name . ".php");
});
Protip here is `dirname(__FILE__)` spl_autoload_register(function($class_name) {
require(dirname(__DIR__) . "/classes/" . $class_name . ".php");
});This post reminded me of them and it'll help me neaten up some of my code. Thanks!