【发布时间】:2015-07-08 10:23:04
【问题描述】:
我是 Lithium PHP 的新手,了解 Lithium 的基础知识。我想实现我自己的Utility 函数。我创建了与app\controllers 平行的utilities 文件夹。这就是我的班级的样子:
<?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];
}
}
?>
我在我的控制器中使用它:
<?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');
}
}
?>
这是正确的做法吗?我的实用程序类是否需要扩展某些东西?或者 Lithium 是否提供了其他方式来实现这一点?
此外,我无法在我的 getItems 方法中访问受保护的变量 $_items。我在我的控制器中实现了同样的东西,然后它工作得很好。
【问题讨论】:
标签: php oop model-view-controller web lithium