Created
July 5, 2017 11:27
-
-
Save PCianes/f6d378de9311f862b7cd889fcdba7378 to your computer and use it in GitHub Desktop.
PHP Object-Oriented Programming (OOP) for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User Blueprint | |
* | |
* @package PCianes\OOPSandbox | |
* @since 1.0.0 | |
* @author Pablo Cianes | |
* @link https://pablocianes.com | |
* @license GNU-2.0+ | |
*/ | |
namespace PCianes\OOPSandbox; | |
class User { | |
protected static $number_of_users = 0; | |
public static $user_id; | |
public static $first_name; | |
protected static $last_name; | |
protected static $email; | |
protected static $twitter; | |
protected static $facebook; | |
protected static $bio; | |
protected static $password; | |
public function __construct() { | |
} | |
public static function create_new( $user_config ) { | |
d( "creating {$user_config['first_name']}'s new user profile" ); | |
self::$user_id = $user_config['user_id']; | |
self::$first_name = $user_config['first_name']; | |
self::$last_name = $user_config['last_name']; | |
self::$email = $user_config['email']; | |
self::$twitter = $user_config['twitter']; | |
self::$facebook = $user_config['facebook']; | |
self::$password = $user_config['password']; | |
self::$number_of_users ++; | |
} | |
public static function getNumberOfUsers() { | |
return self::$number_of_users; | |
} | |
// update profile | |
public static function update_profile( $user_config ) { | |
self::change_password( $user_config['user_id'], $user_config['password'] ); | |
} | |
// change password | |
protected static function change_password( $user_id, $password ) { | |
d( "changing password for {$user_id} to {$password}" ); | |
} | |
// is the user logged in | |
public static function is_logged_in() { | |
} | |
// check access rights | |
public static function has_access( $access_to_what ) { | |
} | |
// save to database | |
protected static function save_to_database() { | |
} | |
} | |
// A class that is all static properties and methods is not OOP. | |
// It’s a design pattern to wrap up like functionality while utilizing the class’ ability to hide complexity. | |
// Some examples of plugins that use the static Class Wrapper design pattern are Gravity Forms and Beaver Builder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constants belong to the class blueprint and not the objects. It’s a constant value for all objects. | |
// It’s not bound to each object. self:: is the internal way to reference a property or method on the class blueprint, | |
// whereas $this represents the object that is using the class code. | |
// http://php.net/manual/en/language.oop5.constants.php | |
// A static is bound to the class blueprint and not the object or any of the objects. | |
// Therefore, all of the objects share the static code. If the class has a static property, | |
// then any object that changes that property is reflected and changed for every single object of that class. | |
// You access the static with a double colon ::. | |
// Within the class, you can use either the class name or the keyword self, | |
// as self is a placeholder for the fully qualified class name. External to the class, you use the class name. | |
<?php | |
/** | |
* OOP Sandbox Plugin | |
* | |
* @package PCianes\OOPSandbox | |
* @author Pablo Cianes | |
* @license GPL-2.0+ | |
* | |
* @wordpress-plugin | |
* Plugin Name: OOP Sandbox Plugin | |
* Plugin URI: https://pablocianes.com | |
* Description: OOP Sandbox test plugin | |
* Version: 1.0.0 | |
* Author: Pablo Cianes | |
* Author URI: https://pablocianes.com | |
* Text Domain: journals | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
*/ | |
namespace PCianes\OOPSandbox; | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit( 'Cheatin’ uh?' ); | |
} | |
function autoload() { | |
include( __DIR__ . '/src/class-static-user.php' ); | |
} | |
function launch() { | |
autoload(); | |
$pablo = create_pablo_user_profile(); | |
$sally = create_sally_user_profile(); | |
// d( $pablo::$first_name ); | |
// d( $sally::$first_name ); | |
// d( User::$first_name ); | |
// | |
// ddd( User::getNumberOfUsers() ); | |
die(); | |
} | |
function create_pablo_user_profile() { | |
echo '======== Pablo\'s profile =========='; | |
$config = array( | |
'user_id' => 1, | |
'first_name' => 'Pablo', | |
'last_name' => 'Cianes', | |
'email' => 'pablo@pablocianes.com', | |
'twitter' => '@pablocianes', | |
'facebook' => '', | |
'password' => 'WordPress Rocks', | |
); | |
$pablo = new User(); | |
d( $pablo ); | |
$pablo::create_new( $config ); | |
d( $pablo ); | |
return $pablo; | |
} | |
function create_sally_user_profile() { | |
echo '======== Sally\'s profile =========='; | |
$config = array( | |
'user_id' => 2, | |
'first_name' => 'Sally', | |
'last_name' => 'Jones', | |
'email' => 'sally.jones@gmail.com', | |
'twitter' => '@sallyjones', | |
'facebook' => '', | |
'password' => 'Know the Code Rocks', | |
); | |
$sally = new User(); | |
d( $sally ); | |
$sally->create_new( $config ); | |
d( $sally ); | |
return $sally; | |
} | |
launch(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* User Blueprint | |
* | |
* @package PCianes\OOPSandbox | |
* @since 1.0.0 | |
* @author Pablo Cianes | |
* @link https://pablocianes.com | |
* @license GNU-2.0+ | |
*/ | |
namespace PCianes\OOPSandbox; | |
class User { | |
protected $user_id; | |
protected $first_name; | |
protected $last_name; | |
protected $email; | |
protected $twitter; | |
protected $facebook; | |
protected $bio; | |
public function create_new( ) { | |
} | |
// update profile | |
public function update_profile() { | |
d( 'updating profile for ' . $this->first_name ); | |
} | |
// change password | |
protected function change_password() { | |
d( 'change password for ' . $this->first_name ); | |
} | |
// is the user logged in | |
public function is_logged_in() { | |
} | |
// check access rights | |
public function has_access( $access_to_what ) { | |
} | |
// save to database | |
protected function save_to_database() { | |
} | |
} | |
// $this is used only within the internal code of an object, i.e. within the class blueprint that built the respective object. | |
// e.g. $this->{$property} = $value;. This technique saves you code and redundancy. | |
class User { | |
protected $user_id; | |
protected $first_name; | |
protected $last_name; | |
protected $email; | |
protected $twitter; | |
protected $facebook; | |
protected $bio; | |
public function __construct( array $config ) { | |
foreach( $config as $property => $value ) { | |
$this->{$property} = $value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// There are 5 different techniques to do make a static method a callback when within the class blueprint itself. | |
//..code left out for brevity | |
public function __construct( array $config ) { | |
foreach ( $config as $property => $value ) { | |
$this->{$property} = $value; | |
} | |
self::$number_of_users++; | |
// 5 different ways to dynamically call a static method callback within a class blueprint: | |
$number_of_users = call_user_func_array( array( 'self', 'getNumberOfUsers' ), array( 201 ) ); | |
// $number_of_users = call_user_func_array( array( 'PCianes\OOPSandbox\User', 'getNumberOfUsers' ), array( 201 + self::$number_of_users ) ); | |
// $number_of_users = call_user_func_array( array( $this, 'getNumberOfUsers' ), array( 201 ) ); | |
// $number_of_users = call_user_func_array( 'PCianes\OOPSandbox\User::getNumberOfUsers', array( 201 ) ); | |
// $number_of_users = call_user_func_array( 'self::getNumberOfUsers', array( 201 ) ); | |
d( $number_of_users ); | |
} | |
// There are 3 different techniques to do it external to the class blueprint: | |
//..code left out for brevity | |
$sally = new_user( get_config( 'sally' ) ); | |
$tonya = new_user( get_config( 'tonya' ) ); | |
//$sally->update_profile(); | |
//$number_of_users = User::getNumberOfUsers(); | |
// 3 different ways to call a static callback outside of the class blueprint. | |
$number_of_users = call_user_func_array( array( 'PCianes\OOPSandbox\User', 'getNumberOfUsers' ), array( 201 ) ); | |
//$number_of_users = call_user_func_array( array( $sally, 'getNumberOfUsers' ), array( 201 ) ); | |
//$number_of_users = call_user_func_array( 'PCianes\OOPSandbox\User::getNumberOfUsers', array( 201 ) ); | |
// *************************************** | |
//Other example for registering an Object’s Method to a WordPress Event Hook | |
//..code removed for brevity | |
add_action( 'init', __NAMESPACE__ . '\check_before_running_our_object', 0 ); | |
function check_before_running_our_object() { | |
global $wp_filter; | |
d( $wp_filter['init'] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment