-
Notifications
You must be signed in to change notification settings - Fork 0
Standardize Classes Writing and Calling
O2Glob has a factory to make you more easy when building a class.
- Basics Factory is for building common class with mixed static and non-static methods and properties.
- Statics is for building a class with all static method function and property within the class
O2Glob is keep maintaining the visibility of your class the protected methods cannot be call outside the class, but for the protected properties you have an on-off access switch. The private methods and properties is strictly not allowed to be called outside the class.
O2Glob has a magic factory to make your class is more easy when you wish to call a method or a property of your class. Basically non-static methods cannot be called as a static methods, but thanks to the magic of O2Glob Magic Factory is now possible, just simple as use '_' (underscore) operator, for example Class::_method() this will call a non-static method().
How about the properties magics? Of course there is a magic also for the class properties. With O2Glob Magic Factory you can call properties in many way.
[1]: Basic knowledge to instantiate a class
$class =& Class::_init(); // equal to new Class();
$class =& Class::get_instance(); // equal to Class::_init()
[2]: Standard call property technique
$class =& Class::_init();
$class->my_non_static_property;
$class::my_static_property;
[3]: Universal Call for static and non-static properties, is call like a method call technique
// Init into a variable
$class =& Class::_init();
$class->my_non_static_property() or $class->my_static_property()
$class::my_non_static_property() or $class::my_static_property()
// Without Init into a variable
Class::my_non_static_property() or Class::my_static_property()
For second type calling method has more functionality on getting the result of your properties especially for array or objects value
// Get The Index
Class::property('index-of-array-object'); // return the value of $property['index']
// Get The Second level Index
Class::property('index','second-index'); // return the value of $property['index']['second-index']
// Get Different type of results as a Array | Object | JSON | Serialize-Array
Class::property('index','typeof'); // typeof = array | object | json | serialize-array
Class::property('index','second-index','typeof');
[4]: Get Class Property
Class::get('name');
$class::get('name');
Class::get('name',['index','typeof');
$class::get('name',['index','typeof');
Class::get('name',['index','sub-index','typeof');
$class::get('name',['index','sub-index','typeof');
[5]: Set Class Property
Class::set($item, $value);
$class::set($item, $value);
$class->item = $value;
Cool isn't it?
To started built a basic singleton class is very easy with O2Glob, example:
namespace App\Libraries;
class My_Class
{
use \O2Glob\Factory\Basics;
// This property flag is to prevent or allowed protected properties to be called
protected $_access_protected_properties = TRUE;
// Initialize is replacement function and same functionality with __construct()
// Why it's change into __initialize(), because the __construct() method is protected by O2Glob Factory
// To prevent re-instantiate
public function __initialize()
{
// Your constructor logic
}
public function non_static_method()
{
// Your method logic
}
public static function static_method()
{
// Your method logic
}
}
To started built a basic singleton class is very easy with O2Glob, example:
namespace App\Libraries;
class My_Class
{
use \O2Glob\Factory\Statics;
// This property flag is to prevent or allowed protected properties to be called
protected $_access_protected_properties = TRUE;
// Initialize is replacement function and same functionality with __construct()
// Why it's change into __initialize(), because the __construct() method is protected by O2Glob Factory
// To prevent re-instantiate
public static function __initialize()
{
// Your constructor logic
}
public static function static_method()
{
// Your method logic
}
private static function _private_static_method()
{
// Your method logic
}
}
// Always write visibility class key
public function method(){}
protected function _protected_method(){} // Always use single underscore at the beginning of your protected method name
private function __private_method(){} // Always use double underscore at the beginning of your private method name
// Always write visibility property key
public $property;
public static $property;
// Always use single underscore at the beginning of your protected property name
protected $_property;
protected static $_property;
// Always use single or double underscore at the beginning of your private property name
private $__property;
private static $__property;
// Combine of PSR-0 and PSR-4 Namespace Class Naming
\App\Libraries\Class_Name
--> this will automatically convert to path root-path/App/Libraries/Class_Name.php
If you wish to add more functionality or have easiest way to do coding.. Please feel free to contact us.