Joshua's Cheatsheets - PHP - Assorted Notes
Light
help

Pulling In and Referencing Other Files

Include and Require Method Calls

WARNING: Avoid using relative paths whenever possible. A common approach (example) is to define constants / globals that hold absolute paths that can be shared across your environment, so you don't always have to do a lookup with dirname(__FILE__) all the time.

NOTE: For both include and require, parenthesis around file name are not required, nor recommended.

Include vs Require

Short answer: require will throw a fatal error (halt execution) if it can't load the file, whereas include will not.

How do I...

  • Initialize an array:

    • Simple (numerical indexed): $myArr = array("alpha", "bravo");
    • Associative:

      $myArr = array(
      	"primaryColor" => "red",
      	"secondaryColor" => "blue"
      );
      
      // Or, with v > 5.4
      $myArr = [
      	"primaryColor" => "red",
      	"secondaryColor" => "blue"
      ];
  • Get the length of something

Objects

The PHP manual has a good guide on objects in PHP.

Instantiating Objects in PHP

There are multiple options for how to instantiate an "object" in PHP.

  • Option: Create a class with public members, and call with new

    class MyClass {
    	public $primaryColor = 'red';
    }
    $myObj = new MyClass();
  • Option: Cast an associative array to an object:

    $myObj = (object) array(
    	'primaryColor' => 'red'
    );

For creating an empty object (for example, as a bucket for future values), you can use the methods above, but in a streamlined way:

  • You can use $emptyObj = new stdClass();
  • You can use $emptyObj = (object) [];

VSCode

PHP, like some other languages out there, has a user-base that has been kind of slow to adopt VSCode as an IDE. As a bit of a self-fulfilling prophecy (why build something if no one is going to use it?), PHP support in VSCode is a little lackluster (at least at the moment).

In general, the PHP community tends to use software from the JetBrains company; particularly PhpStorm. To be fair to PHP devs, PhpStorm has been around since 2009, while VSCode has only been around since 2015.

WordPress Editing in VSCode

I recommend either:

  • Edit your theme / plugin by opening an entire Wordpress installation directory (e.i., the root folder that contains wp-config.php), so VSCode can pick up the function declarations

    • And / Or...
  • Install the WordPress Snippets extension

Formatting PHP in VSCode

One of the most popular extensions for PHP in VSCode is PHP Intelephense by Ben Mewburn. This can function as a formatter, but it has limited controls when it comes to that (for example, disabling Allman braces is not yet an option).

An alternative is phpfmt - PHP formatter. Although it is no longer maintained (😢), it is one of the few PHP formatting extensions, and it still works. For example, if you want to default to K&R style braces instead of Allman, you can use these settings.

Debugging

XDebug

XDebug is the best solution. Usually all it takes to enable is to download the matching DLL (use this tool to find matching version) and place in extension folder. Then add these lines to php.ini config file (example):

zend_extension=xdebug-2.7.2-7.2-vc15-x86_64
xdebug.remote_enable=1
xdebug.remote_autostart=1

In VSCode, this is usually the config I use in launch.json for debugging:

{
	"name": "Listen for XDebug",
	"type": "php",
	"request": "launch",
	"port": 9000
}

Warning: Adding XDebug will usually add some additional load time in your local environment

Manually triggering XDebug

Aside from adding breakpoints, you can also trigger your IDE debugger manually (similar to JavaScript debugger; kw), by using xdebug_break() (details). Just make sure to not have in production code!!!

Markdown Source Last Updated:
Tue Jul 07 2020 22:36:58 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 19 2019 17:06:24 GMT+0000 (Coordinated Universal Time)
© 2020 Joshua Tzucker, Built with Gatsby