简体   繁体   中英

Storing a reference to a class function in a variable in PHP

Here is a hypothetical example (the parent class PageState , contains an instance of the class FooterState - the instance may not be created, depending on the conditions. The FooterState needs to call a function which is public and is created in the PageState class):

class PageState {
    private $footer_state = null;

    function PageState() {
        $this->footer_state= new FooterState($this);
    }

    public function getExpectedPageDimensions() {
        // do calculations based on existing body content
        return $dimensions;
    }
}

class FooterState {
    private $get_dimensions_func = null;

    function FooterState($page_state) {
        // Here, we need to get the reference to the function from the $page_state class
        $this->get_dimensions_func = $page_state->getExpectedPageDimensions;
    }

    public function addLogos($logo_data) {
        $page_dimensions = $this->get_dimensions_func();
        // use the page dimensions to decide on the size of the content
        return Array('width' => $width, 'height' => $height);
}

I am aware of alternative solutions:

  • Instead of making a copy of the reference to the function, create a refference to the class $this->page_state = $page_state; and then functions in FooterState can call $this->page_state->getExpectedPageDimensions();
  • Use global $PageStateInstance; and then just call $PageStateInstance->getExpectedPageDimensions();

But I am wondering if it is at all possible to store a reference to a class function in a variable. If the functions were outside of the class, it would be possible to do stuff like $func = 'getExpectedPageDimensions'; $func(); $func = 'getExpectedPageDimensions'; $func(); .

You can pass on an instance plus a function as a callable : An array with the instance and the function name. There is a similar system for calling static class methods.

# An example callback method
class MyClass {
    function myCallbackMethod() {
        echo 'Hello World!';
    }
}

# create an instance
$obj = new MyClass();
# and later:
call_user_func(array($obj, 'myCallbackMethod'));

From the docs here: http://php.net/manual/en/language.types.callable.php

Instead of making a copy of the reference to the function, create a refference to the class $this->page_state = $page_state; and then functions in FooterState can call $this->page_state->getExpectedPageDimensions();

This is the best generic solution.

But I am wondering if it is at all possible to store a reference to a class function in a variable.

Yes it is, but it really only works for static functions unless you instantiate the class. Example:

class A {
    public static function doSomethingStatic() {
        // ...
    }
    public function doSomethingElse() {
        // ...
    }
}

$somevar = 'A::doSomethingStatic';
$result = call_user_func($somevar); // calls A::doSomethingStatic();

$myA = new A();
$myref = array($myA, 'doSomethingElse');
$result = call_user_func($myref); // calls $myref->doSomethingElse();

Note that in the second example you have to instantiate the class and pass an array as the first parameter to call_user_func() .

References: http://php.net/manual/en/function.call-user-func.php and http://php.net/manual/en/language.types.callable.php

is at all possible to store a reference to a class function

I think you mean object instead of class , but yes you can, with closures.

I don't think you need to though. $this->page_state seems like it'll work just fine.

Don't use globals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM