PHP:我可以将函数存储在类对象中,并向其传递值以供函数解释。Python可以做到,但是PHP可以吗?

菲利普·李

我很难弄清楚如何实现类似于以下Python代码的代码:

class _Mapper(object):
    # Holds the name of the function to call when we want to predict a value.
    # There may be dozens of these _Mapper objects stored in an array which are
    # created by calling _create_couplet() in a nested loop
    def __init__(self, estimator):
        self.estimator = estimator

def _create_couplet(pairings, A, B):
    # Gets the data for the pairing from the pairings class and returns a Mapper
    # object containing the function (either function 1 or function2 - defined in
    # pairing class) that we'll use to to predict the value
    if A < B:
        return (_Mapper(pairings.function1))
    else:
        return (_Mapper(pairings.function2))

# Use the function (either function1 or function2) stored in _Mapper.estimator
# (created by _create_couplet) to estimate the new value based on the old value
newValue = _Mapper.estimator(oldValue)

本质上,它创建_Mapper对象的数组,并且每个对象都包含对函数(function1或function2)的引用,该引用随后用于计算值。关键步骤似乎是:

newValue = _Mapper.estimator(oldValue)

它采用_Mapper.estimator中保存的函数,然后将oldValue传递给它,就像它是newValue = function1(oldValue)或newValue = function2(oldValue)

我正在尝试在PHP中实现类似的功能,但是在CLI中运行此功能时似乎出现了某种无限循环。似乎函数名称未存储在

class Pairings {
    // Define the two functions (these are just placeholders)
    public function function1($value = null) {
        return $value * 2;
    }

    public function function2($value = null) {
        return $value * 4;
    }
}

class _Mapper {
    function __construct($estimator) {
        // This should store the function that we are going to
        // use based on the value of A & B in _create_couplet()
        $this->estimator = $estimator;
    }

    function estimator($value) {
        // This should pass the value specified to either function1()
        // or function function2() and should return the result.  It
        // doesn't !
        return $this->estimator($value);
    }
}

function _create_couplet($pairing, $A, $B) {
    // Based on the value of A or B store the appropriate function
    if ($A < $B) {
        return (new _Mapper($pairing->function1()));
    } else {
        return (new _Mapper($pairing->function2()));
    }
}

// Set up some values
$oldValue = 10;
$A = 10;
$B = 5;

// Define the pairing
$pairing = new Pairings;

// Create the _Mapper object
$couplet = _create_couplet($pairing, $A, $B);

// Pass oldValue into the appropriate function and get the result back
$newValue = $couplet->estimator($oldValue);
echo ("New value: $newValue");

我确定我犯了一个简单的错误,但看不到我错过的内容。如何将函数名称存储在对象中,然后将参数传递给对象,以便存储的函数使用该参数并返回值?

克恩

有两个问题:

在您的_create_couplet方法中,您要提供的参数new _Mapper()不是要执行的函数,而是函数的结果。您正在执行$pairing->function1()执行null * 2是因为您允许null值,否则它应该失败,因为您正在调用function1而没有参数。

正如Lars建议的那样,您应该使用一个可调用的参数:

//before
new _Mapper($pairing->function1());

//after
new _Mapper([$pairing, 'function1']);

引发无限循环的第二个问题是您尝试在estimator方法中调用此callable ,即call $this->estimator()您可能会认为$this->estimator()引用包含在中的可调用对象$this->estimator,但是由于您有一个具有相同名称的方法,因此PHP会认为您正在调用该方法而不是属性。

修复它的最简单方法是用不同的方式命名方法或属性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章