简体   繁体   English

在Lithium中实现我自己的实用程序功能?

[英]Implementing my own utility functions in Lithium?

I am a newbie to Lithium PHP and know the basics of Lithium. 我是Lithium PHP的新手,并且了解Lithium的基础知识。 I want to implement my own Utility functions. 我想实现自己的Utility功能。 I have created the utilities folder parallel to app\\controllers . 我创建了与app\\controllers平行的utilities文件夹。 This is how my class looks like: 这是我的班级样子:

<?php

namespace app\utilities;

class UtilityFunctions {
    protected $_items = array(
            'a' => 'Apple',
            'b' => 'Ball',
            'c' => 'Cat'
        );

    //I get _items as undefined here, blame my poor OOP skills.
    public function getItem(alphabet) {
        return $this->_items[alphabet];
    }
}

?>

I use it in my controller as: 我在控制器中将其用作:

<?php

namespace app\controllers;

use \app\utilities\UtilityFunctions;

class MyController extends \lithium\action\Controller {
    public function index() {
        $args = func_get_args();
        $item = UtilityFunctions::getItem($args[0]);

        return compact('item');
    }
}

?>

Is this the right way to do it? 这是正确的方法吗? Does my utility class need to extend something? 我的实用程序类是否需要扩展某些内容? Or does Lithium provide some other way to achieve this? 还是锂提供了其他方法来实现这一目标?

Moreover, I am not able to access the protected variable $_items in my getItems method. 此外,我无法在我的getItems方法中访问受保护的变量$_items I had the same thing implemented in my controller and it worked fine then. 我在控制器中实现了相同的功能,然后效果很好。

This is a fine way to do it. 这是一种很好的方法。 There are two core Lithium classes that you could extend from: \\lithium\\core\\Object and \\lithium\\core\\StaticObject 您可以从中扩展两个核心的锂电池类: \\ lithium \\ core \\ Object\\ lithium \\ core \\ StaticObject

I think that is not necessary for your use case. 我认为这对于您的用例不是必需的。

The reason you cannot access the protected variable is because you are invoking the method statically. 无法访问受保护变量的原因是因为您是静态调用该方法的。 You should probably rewrite your class to use static variables and methods. 您可能应该重写类以使用静态变量和方法。 Also, I assume it's just a typo creating an example for StackOverflow, but you forgot the $ for the alphabet var in the getItem method. 另外,我假设这只是为StackOverflow创建示例的错字,但是您忘记了getItem方法中alphabet var的$

<?php

namespace app\utilities;

class UtilityFunctions {
    protected static $_items = array(
        'a' => 'Apple',
        'b' => 'Ball',
        'c' => 'Cat'
    );

    public static function getItem($alphabet) {
        return static::_items[$alphabet];
    }
}

?>

The other option is to not change the UtilityFunctions class, but instead instantiate the class in the controller: 另一个选择是不更改UtilityFunctions类,而是在控制器中实例化该类:

<?php

namespace app\controllers;

use app\utilities\UtilityFunctions;

class MyController extends \lithium\action\Controller {
    public function index() {
        $args = func_get_args();
        $utilities = new UtilityFunctions();
        $item = $utilities->getItem($args[0]);

        return compact('item');
    }
}

?>

For a simple case like this where the class does not need to hold any state, I would recommend the static methods. 对于此类不需要类保持任何状态的简单情况,我建议使用静态方法。 Take a look at the php manual for a little more info on the static keyword. 看看php手册 ,了解有关static关键字的更多信息。

Another minor note, you do not need the leading backslash in your use statement. 另一个小注释,您在use声明中不需要反斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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