Skip to content

Instantly share code, notes, and snippets.

@yesnik
Last active September 15, 2017 06:29
Show Gist options
  • Save yesnik/4731d67fc8b0fbe891e299d79fe30538 to your computer and use it in GitHub Desktop.
Save yesnik/4731d67fc8b0fbe891e299d79fe30538 to your computer and use it in GitHub Desktop.
PHP OOP details

PHP Visibility Modifiers

Protected static functions

class Hello {
    
    public static function greet($name) {
        return self::getMessage($name);
    }
    
    protected static function getMessage($name) {
        return 'Hello ' . $name;
    }
}

echo Hello::greet('Kenny'); // Hello Kenny
echo Hello::getMessage('Kenny'); // Fatal error:  Call to protected method Hello::getMessage() from context

Protected static properties

class Hello {
    protected static $USER_TYPES = ['admin', 'content'];
    
    public static function getUserTypes() {
        return self::$USER_TYPES;
    }
}

var_dump(Hello::getUserTypes());
/*
array(2) {
  [0]=>
  string(5) "admin"
  [1]=>
  string(7) "content"
}
*/

Hello::$USER_TYPES; // Fatal error</b>:  Cannot access protected property Hello::$USER_TYPES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment