Skip to content

Commit

Permalink
CS: review of all include and require statements
Browse files Browse the repository at this point in the history
`include` and `require` are language constructs, not functions.

With that in mind there are a number of best practices surrounding them:
* There is no need to use parenthesis and not doing so will be, albeit marginally, faster.
* Always pass an absolute path for maximum portability.
    Using `dirname(__FILE__)` instead of `__DIR__` to maintain compatibility with PHP 5.2.
* Use `require_once` instead of `include` when the file is _required_ for the rest of the script to be able to function.
  • Loading branch information
jrfnl committed Sep 19, 2019
1 parent 3470169 commit 7e392df
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/basic-auth.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/cookie.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/cookie_jar.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/get.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/multiple.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/post.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/proxy.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/session.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion examples/timeout.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// First, include Requests
include('../library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';

// Next, make sure Requests can load internal classes
Requests::register_autoloader();
Expand Down
2 changes: 1 addition & 1 deletion library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static function autoloader($class) {

$file = str_replace('_', '/', $class);
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
require_once(dirname(__FILE__) . '/' . $file . '.php');
require_once dirname(__FILE__) . '/' . $file . '.php';
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function define_from_env($name, $default = false) {
define_from_env('REQUESTS_HTTP_PROXY_AUTH_USER');
define_from_env('REQUESTS_HTTP_PROXY_AUTH_PASS');

include(dirname(dirname(__FILE__)) . '/library/Requests.php');
require_once dirname(dirname(__FILE__)) . '/library/Requests.php';
Requests::register_autoloader();

function autoload_tests($class) {
Expand All @@ -39,7 +39,7 @@ function autoload_tests($class) {
$class = substr($class, 13);
$file = str_replace('_', '/', $class);
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
require_once(dirname(__FILE__) . '/' . $file . '.php');
require_once dirname(__FILE__) . '/' . $file . '.php';
}
}

Expand Down

0 comments on commit 7e392df

Please sign in to comment.