1

i want make a static method like this:

Class MYClass{
    public static $text = 'apple';
    public static $text2 = ' & banana';

    public static function get($var){

        return static::$var;

    }
    public static function with($var){

        return static::$var;

    }

}

if i call :

$var = MYClass::get('text');

$var will be returned 'apple'

and if i call:

$var = MYClass::get('text')->with('text2');

$var will be returned 'apple & banana'

how to make that works?

  • 7
    You can't, unless somehow your static returns an instance, because `->` is an instance operator – Mark Baker Feb 11 '16 at 17:00
  • yes with 'return new static()', but how to check if 'static with method' is called? – Jimmy Wijaya Feb 11 '16 at 17:02
  • Static methods can't be chained (in a nice way) and very much bad practice to return an instance. I would recommend you do as above answer and implement as a singleton. – Brian Feb 11 '16 at 17:02
  • MYClass::get('text') returns a string. You are trying 'apple'->with('text2'). If you want chaining, MYClass::get('text') must return an object that has a method with() – Mihai Răducanu Feb 12 '16 at 09:26

0 Answers0