Skip to content

Getting Started

ookhrimenko edited this page Jan 16, 2018 · 5 revisions

If you have never worked with PHP Frameworks and have experience only with CMS's like WordPress or Drupal, then we recommend learning several technical topics, which will help you to understand other documentation articles better.

  • Object-oriented code
  • Namespaces and autoload
  • MVC pattern

You can find a short description of each topic and related links below.

Once you're done learning these topics, you can try and install our theme to see how it's built and what features it's packed with.

Object-Oriented Code for WordPress

The object-oriented code has a lot of advantages over old procedural code style. A lot of WordPress developers are already using these principles to build their themes and plugins.

If you are new to OOP, we recommend reading some articles about it first: – WPMUDEVEnvato Tuts+WP Engine

Object-oriented architecture (based on SOLID):

– One PHP source file per class definition. – One class should represent one feature or a component (however can include other classes, if needed). – Group class files in folder by logic meaning (post type, model, taxonomy, etc.) – Use namespaces to keep class names clean. – We follow WordPress Coding Standards for class naming. However, we follow the PSR-2 standard for file naming (because it's easier to read/navigate component names as file names).

Classes structure principles

– If your class registers any hooks, it should implement a singleton design pattern (to be able to remove the hook if necessary) – All hooks should be registered in class construct method and take the public method of the current object as a callback parameter. – Hook registration is activated by creating a class instance somewhere in the code.

An object-oriented example of hook registration:

Admin/Hello_World_Page.php (class declaration)

<?php
namespace MyVendor\MyPackage\Admin;

use JustCoded\WP\Framework\Objects\Singleton;

class Hello_World_Page {
	use Singleton;

	protected function __construct() {
		add_action( 'admin_menu', array($this,'admin_menu') );
	}

	public function admin_menu() {
		add_options_page( 'My Options', 'My Options', 'manage_options', 'my-feature', array($this, 'render_page') );
	}
	
	public function render_page() {
		echo 'Hello World!';
	}
}

// create an instance and run hooks registration.
Hello_World_Page::instance();

functions.php (or another file, which is loaded during the page load)

<?php
// ...

// create an instance and run hooks registration.
Hello_World_Page::instance();

Namespaces and Autoload

Namespaces are named program regions used to limit the scope of variables inside the program. They are used in many programming languages to create a separate region for a group of variables, functions, classes, etc.

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.

Official PHP namespaces documentation is explaining how to use it.

So namespaces are very similar to directories, usually based on namespace path application auto-include PHP files. It's much easier than remembering which files you need to include() or require() during bootstrap application. Autoload is also a standard PHP feature: spl autoload.

Except knowing what namespaces and autoload are, you need to know that there is a global standard of "naming" your namespaces. It's called PSR-4: Autoloader.

So usually namespaces are defined as:

  • \<Vendor>\<Package>(\<Subnamespaces>)*\<ClassName>
  • or <Well-known Package>(\<Subnamespaces>)*\<ClassName>

MVC Pattern

We don't use the full MVC pattern in our theme (since we don't forget we're developing inside a WordPress 😃), however, we use some ideas from it.

Understanding MVC pattern.

Our theme has 3 main parts/concepts:

  • View is where the data, requested from the Model, is viewed and its final output is determined (the HTML is generated and displayed). WordPress is handling routing and primary data load. If we need additional data on the page, we create Models.
  • Model is the name given to the permanent storage of the data used in the overall design. It must allow access for the data to be viewed, or collected.
  • Also we have Components, which extend the default application (WordPress).

Next: Installation