【发布时间】:2012-05-18 05:18:06
【问题描述】:
我像这样创建我的组件
<?php
class UtilComponent extends Object
{
function strip_and_clean( $id, $array) {
$id = intval($id);
if( $id < 0 || $id >= count($array) ) {
$id = 0;
}
return $id;
}
}
并像这样使用它
var $name = 'Books';
var $uses = array();
var $components = array('Util');
public function index($id = 0){
$this -> set('page_heading','Packt Book Store');
$book=array(
'0'=>array(
'bookTitle' =>'Object Oriented Programming with PHP5',
'author' =>'Hasin Hayder',
'isbn' => '1847192564',
'releaseDate' => 'December 2007'
),
'1'=>array(
'bookTitle' =>'Building Websites with Joomla! v1.0',
'author' =>'Hagen Graf',
'isbn' => '1904811949',
'releaseDate' => 'March 2006'
),
);
$id = $this->Util->strip_and_clean( $id, $book);
$this->set('book',$book[$id]);
$this->pageTitle='Welcome to the Packt Book Store!';
}
但我得到了这个错误
**call_user_func_array() 期望参数 1 是有效回调,类 'UtilComponent' 没有方法 'initialize' [CORE\Cake\Utility\ObjectCollection.php,第 130 行]**
【问题讨论】:
-
尝试打印_r($id);给我看……
-
尝试在行前打印 $id = $this->Util->strip_and_clean( $id, $book);在此之后也退出;展示给我们。
标签: cakephp components