8

Due to the fact that you can not use $this-> inside a static functio, how are you supposed to access regular functions inside a static?

private function hey()
{
    return 'hello';
}

public final static function get()
{
    return $this->hey();
}

This throws an error, because you can't use $this-> inside a static.

private function hey()
{
    return 'hello';
}

public final static function get()
{
    return self::hey();
}

This throws the following error:

Non-static method Vote::get() should not be called statically

How can you access regular methods inside a static method? In the same class*

Jony Kale
  • 111
  • 1
  • 1
  • 5

3 Answers3

9

Static methods can only invoke other static methods in a class. If you want to access a non-static member, then the method itself must be non-static.

Alternatively, you could pass in an object instance into the static method and access its members. As the static method is declared in the same class as the static members you're interested in, it should still work because of how visibility works in PHP

class Foo {

    private function bar () {
        return get_class ($this);
    }

    static public function baz (Foo $quux) {
        return $quux -> bar ();
    }
}

Do note though, that just because you can do this, it's questionable whether you should. This kind of code breaks good object-oriented programming practice.

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Actually it is a quite valid pattern for factories. That is also a way to overload constructors... In some sense. Factory + various init functions. – Nux Jan 27 '21 at 19:39
7

You can either provide a reference to an instance to the static method:

class My {
    protected myProtected() {
        // do something
    }

    public myPublic() {
        // do something
    }

    public static myStatic(My $obj) {
        $obj->myProtected(); // can access protected/private members
        $obj->myPublic();
    }

}

$something = new My;

// A static method call:
My::myStatic($something);

// A member function call:
$something->myPublic();

As shown above, static methods can access private and protected members (properties and methods) on objects of the class they are a member of.

Alternatively you can use the singleton pattern (evaluate this option) if you only ever need one instance.

Community
  • 1
  • 1
Lukas
  • 1,479
  • 8
  • 20
0

I solved this by creating an object of the concerning class inside a static method. This way i can expose some specific public functions as static function as well. Other public functions can only be used like $object->public_function();

A small code example:

class Person
{
  public static function sayHi()
  {
    return (new Person())->saySomething("hi");
  }

  public function saySomething($text)
  {
    echo($text);
  }

  private function smile()
  {
    # dome some smile logic
  }
}

This way you can only say hi if using the static function but you can also say other things when creating an object like $peter->saySomething('hi');. Public functions can also use the private functions this way.

Note that i'm not sure if this is best practice but it works in my situation where i want to be able to generate a token without creating an object by hand each time.

I can for example simply issue or verify a token string like TokenManager::getTokenToken(); or TokenManager::verifyToken("TokenHere"); but i can also issue a token object like $manager = new TokenGenerator(); so i can use functionality as $manager->createToken();, $manager->updateToken(updateObjectHere); or $manager->storeToken();

CodeNinja
  • 836
  • 1
  • 15
  • 38