Skip to content

Getting Started

alex_prokopenko edited this page Jan 12, 2018 · 1 revision

If you never work with PHP Frameworks and have experience only with CMSes (WordPress, Drupal, etc.) then we recommend to learn several technical topics, which helps you to better understand other documentation articles.

  • Object-oriented Code
  • Namespaces and Autoload
  • MVC Pattern

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

Once you complete with learning of these topics - you can try to install our theme and find out how it is build and what features it has.

Object-Oriented Code for WordPress

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 to read some articles about it first:

Object-oriented architecture (based on SOLID):

  • One PHP source file per class definition
  • One class should represent one feature or 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 PSR-2 standard in 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 Singleton design pattern (to be able to remove hook if necessary)
  • All hooks should be registered in class construct method and take public method of current object as a callback parameter
  • Hooks registration is activated by creating a class instance somewhere in the code

Hooks registration object-oriented example:

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 explaining how to use it.

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

Except knowing what is namespaces and autoload, you need to know that there is a world standard about how to "name" your namespaces. It 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 full MVC pattern in final theme build with this framework (we don't forget we're developing inside a WordPress 😃), however we use some ideas from it.

Understanding MVC pattern.

The theme build with this framework has 3 main parts/concepts:

  • View is where 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 extends the default application (WordPress).

Next: Installation