Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a public/private function inside a static function?

Tags:

methods

oop

php

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*

like image 972
Jony Kale Avatar asked May 07 '13 20:05

Jony Kale


People also ask

Can static method access private member?

DR; TL; Yes it can.

Can you access a private non static class field with a public static method of the same class?

A static method can only access static data members and static methods of another class or same class but cannot access non-static methods and variables.

Can static methods be public or private?

Static methods can be public or private. The static keyword is placed right after the public/private modifier and right before the type of variables and methods in their declarations.

Can static function be public?

To call a static method from a child class, use the parent keyword inside the child class. Here, the static method can be public or protected .


2 Answers

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.

like image 136
GordonM Avatar answered Sep 21 '22 07:09

GordonM


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.

like image 40
Lukas Avatar answered Sep 18 '22 07:09

Lukas