Skip to content
Advertisement

Getting properites of static class in static function?

I just wanna get return class properties in not instantiated class use. There is no way to instance this class? Please tell me…! My example is below↓↓

  <?php
    class MyTest {
        public static $test1 = 'a';
        
        public static $test2 = 'b';
        
        public static function getProperties() {
            //how to code here...?
        }
    }
    //plz return $test1, $test2
    MyTest::getProperties();

Advertisement

Answer

Use self:: to access static properties:

public static function getProperties() {
    return [self::$test1, self::$test2];
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement