Having a single line ‘include’ statement which references a different file is in no way the same as having an identical block of code which appears in multiple places.
It 100% is
require_once 'std_table.class.php'
new $table_name;
require_once 'std_table.class.php'
new $table_name;
require_once 'std_table.class.php'
new $table_name;
is 100% repeated code. There’s no argument there.
Since this is its own topic now I’ll elaborate a little. Given this code:
require_once 'my.class.php'
new MyClass;
require_once 'my.class.php'
new MyClass;
You have several issues:
- The classic problem with repeated code: If you move the file location/name you must find all occurrences of the require line and update it
- There is a performance overhead. Each time you call require_once PHP has to look to see whether that file has already been included or not.
- It slows down development. Each time I want to use a class I have to remember its file location and type out the require_once line.
- It mixes concerns. Why should the person using the class need to worry about whether or not it’s been included or where in the file system it sits?
- A chdir() or set_include_path() elsewhere in the script will break things
- It causes action-at-distance and you have to be very careful not to accidentally break code
This code works perfectly even if I forget to put in the require_once line in the B function:
function A() {
require_once 'foo.php';
$foo = new Foo;
}
function B() {
$foo = new Foo;
}
A();
B();
Of course If I reverse the order of method calls it breaks:
function A() {
require_once 'foo.php';
$foo = new Foo;
}
function B() {
$foo = new Foo;
}
B();
A();
You might say “I’d never forget” but it’s easy to do and won’t cause any immediate problems until possibly years down the line when the method calls get reversed.
All of these problems stem from having repeated code. Without the repetition you would never even have to consider these potential problems.
In the interests of creating robust, bugfree software, having a codebase in which it is impossible for these problems to even exist in is a no-brainier considering the alternative (No repeated code) is faster to code, faster to execute and easier to read.
Post edited by cpradio, let’s not assume what someone else may or may not say