我像这样创建我的组件
<?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 行]**