PHP:美丽的动态语言。设计模式

发布于 2024-10-20 03:30:03 字数 858 浏览 1 评论 0原文

我有以下模式。或者更好的想法! 有从 BaseUnit 继承的“unit”类和从 BaseUser 继承的“users”类。后代单位和后代用户在结构上非常相似。

我想为使用单元的用户应用“工具栏”功能

它看起来像

class KickerUser extends BaseUser{
     public $toolbar = array('view()', 'kick("right_leg", "twice")')
}


class BaseUnit{
     public function view();

     public function kick($a, $b);

     public function bite($c);

     protected function applyToolbar($user){

     ///////////////HERE COMES THE TRICK////////
           foreach($user->toolbar as $t){
               $this->$t;
           }
     //  should I use eval() for this
     //  to become something like:  $this->bite('hard');
     //  i'm interested in making the readable code 
     //  and passing constant parameters
     /////////////HOW TO WRTITE THIS CORRECTLY ?
     }
}

这是一种很好的做事方式吗?

I have the following pattern. Or better the IDEA!
There are "unit" classes inherited from BaseUnit and "users" classes inherited from BaseUser. Descendant units and descendant users are pretty similar in structure.

I want to apply a "toolbar" feature for users working with units

It can look like

class KickerUser extends BaseUser{
     public $toolbar = array('view()', 'kick("right_leg", "twice")')
}


class BaseUnit{
     public function view();

     public function kick($a, $b);

     public function bite($c);

     protected function applyToolbar($user){

     ///////////////HERE COMES THE TRICK////////
           foreach($user->toolbar as $t){
               $this->$t;
           }
     //  should I use eval() for this
     //  to become something like:  $this->bite('hard');
     //  i'm interested in making the readable code 
     //  and passing constant parameters
     /////////////HOW TO WRTITE THIS CORRECTLY ?
     }
}

Is this a good way of doing things?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2024-10-27 03:30:04

我本想给你指出卡萨布兰卡给你指出的同一个方向,但他抢先了我:)。

不过,这里有一个您可能会发现有用的小示例:

<?php
class MyClass{
    private $_name;

    public function __construct($name){
        $this->_name = $name;
    }

    private function sayMyName($asker){
        echo 'My name is ', $this->_name, ', ', $asker, '!';
    }

    public function askForName($asker){
        call_user_func(array($this, 'sayMyName'), $asker);
    }
}

$obj = new MyClass('Paul');
$obj->askForName('Mr. Rogers');
?>

祝您好运,
阿林

I was going to point you in the same direction that casablanca pointed you in, but he beat me to it :) .

Nevertheless, here's a little example you may find useful:

<?php
class MyClass{
    private $_name;

    public function __construct($name){
        $this->_name = $name;
    }

    private function sayMyName($asker){
        echo 'My name is ', $this->_name, ', ', $asker, '!';
    }

    public function askForName($asker){
        call_user_func(array($this, 'sayMyName'), $asker);
    }
}

$obj = new MyClass('Paul');
$obj->askForName('Mr. Rogers');
?>

Good luck,
Alin

前事休说 2024-10-27 03:30:03

除非没有更好的解决方案,否则不要使用eval。就您而言,事实证明有一个更好的解决方案 - call_user_funccall_user_func_array。这两个函数之间的区别在于是直接传递参数还是作为数组传递参数。

例如,如果您想调用 $this->kick('right_leg', 'twice'),您可以将其写为:

call_user_func(array($this, 'kick'), 'right_leg', 'twice');

或:

call_user_func_array(array($this, 'kick'), array('right_leg', 'twice'));

您可以轻松地使其动态化:

call_user_func_array(array($this, $methodName), $parametersArray);

Don't use eval unless there isn't a better solution. In your case, it turns out there is a better solution -- call_user_func and call_user_func_array. The difference between the two functions is whether you pass the parameters directly or as an array.

For example, if you want to call $this->kick('right_leg', 'twice'), you could write it as:

call_user_func(array($this, 'kick'), 'right_leg', 'twice');

or:

call_user_func_array(array($this, 'kick'), array('right_leg', 'twice'));

You can easily make this dynamic:

call_user_func_array(array($this, $methodName), $parametersArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文