-1

Is it possible to simply make methods work with both static calls and instantiated object calls.

For example:

class MyClass {
    private static $instance = null;
    private $my_value = 'Foo';

    public function __construct() {
        self::$instance = new self;
    }

    public static function my_function() {
        if( isset( $this ) ) {
            $target = $this->; // I know its an invalid declaration
        } else {
            $target = self::$instance->; // I know its an invalid declaration
        }
        return $target $my_value; // Invalid statement
    }
}

$my_object = new My_Class();
echo $my_object->my_function(); // Should output Foo
echo My_Class::my_function(); // Should output Foo

Hope this is clear

McShaman
  • 3,627
  • 8
  • 33
  • 46
  • 2
    ...why would you ever want to? – Major Productions Feb 13 '14 at 23:14
  • The reason why was because I wanted to see if I could make a singleton object return the same result no matter if the method was called statically or from an instance. – McShaman Feb 14 '14 at 05:52
  • 1
    Well, a singleton, by definition, is an *object* that's created statically, with some constructor shenanigans to ensure only one exists at any given time. Since it's an object, you should treat it as one. Of course, that begs the question why are you trying to use a singleton in an environment that generally has the life cycle of a single request? And passes objects by reference by default? I find singletons to be wholly unnecessary in PHP. Use dependency injection instead. It's a cleaner way to address the problem that doesn't blow apart scope. – Major Productions Feb 14 '14 at 15:15
  • 1
    I appreciate your feedback/opinion. This is not for a commercial application. As with I'm sure you can appreciate I was tinkering with some code and was curious if something, as awkward as it is, is possible. Nobody innovated anything by only doing what is recommended. – McShaman Feb 14 '14 at 21:20

1 Answers1

2

No, this isn't an option.

Methods are either static or non-static.

[As kingkero mentioned, you actually can. But PHP reports an error. Therefore, I wrote you can't. Read: You never ever want to!]

And I can't image a situation, why one would need to call a method using self:: and $this->.

You probably want to implement the single instance pattern explained here:

class CSampleClass
{
    private static $instance;
    private $count = 0;

    private function __construct()
    {
    }

    public static function singleton()
    {
        if (!isset(self::$instance)) {

            self::$instance = new CSampleClass();
        }
        return self::$instance;
    }

    public function increment()
    {
        return $this->count++;
    }

    public function __clone()
    {
        trigger_error('Failed to create the clone', E_USER_ERROR);
    }

    public function __wakeup()
    {
        trigger_error('Sorry, ' . __CLASS__ . ' can"t be deserialized' , E_USER_ERROR);
    }
}
SteAp
  • 11,853
  • 10
  • 53
  • 88
  • Actually you can call static methods in non-static context (and vice versa) - although it won't change the original behaviour and will throw an error/notice. There is some information about this behaviour in PHP online – kero Feb 13 '14 at 23:20
  • @kingkero Yeah you can. And you can jump from a 50m high bridge. Actually, you won't - and I won't propose ;-) Additionally, PHP might eventually forbid to call.. – SteAp Feb 13 '14 at 23:22
  • Thanks for taking the time to respond – McShaman Feb 14 '14 at 05:54