- Steps
- Create a class
- Wrap all variable and classes inside class
- Create a new variable instance of class
- Handle variables and classes through instance of class.
- Create a class
- class ExcellentProgrammingTricks {...}
- New instance of a class
- excellentProgrammingTricks (Camel Case) = new ExcellentProgrammingTricks (Pascal case);
- Extension of class
- class SecondClass extends ExcellentProgrammingTricks{...}
- Final Class
- Can't be extends by another class.
- final class ExcellentProgrammingTricks {...}
- PHP visibility methods on variable or functions
- Public
- Access inside the class itself or outside class by declaring the instance.
- Default
- Accessed everywhere
- Protected
- Accessed only within the class itself or extensions of that class.
- Private
- Access indie the class itself.
- Class instance not work.
- Static method
- Method can be accessed without initialize the class (instance of class)
- Example 1 class ExcellentProgrammingTricks {
static function _myStaticFunction() {
echo "Hi!";
} - ExcellentProgrammingTricks:: _myStaticFunction(); //Use directly without using new instance.
- Example 2- class ExcellentProgrammingTricks {
static function register() {
add_action( 'admin_enqueue_scripts', array( 'ExcellentProgrammingTricks', 'enqueue' ) );} //Use class name instead $this - static function enqueue() { wp_enqueue_style( 'style', plugins_url( '/assets/style.css', __FILE__ ) ); }}
- Function/Method
- Automatically run a function when instance is created.
- function __construct() {
add_action( 'init', array( $this, '_my_post_name' ) );
} - General function
- [public|private|protected] function abc(){...}
- Access function
- $excellentProgrammingTricks->abc();
No comments:
Post a Comment