我想创建一个对象,但我是 OOP 的初学者,尝试了下面的代码并返回错误代码:
解析错误:第 25 行 /home/acosor/work/bpideeas/branches/testing/clasa/clasa.php 中的语法错误、意外 '('、期望 '&' 或变量 (T_VARIABLE)
基本上我想在构造中插入一个函数,但我真的不知道该怎么做。
<?php
$clasa = array(
'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23),
'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27),
'e3' => array('nume' => 'Noman', 'prenume' => 'Alice', 'sex' => 'f', 'varsta' => 22),
'e4' => array('nume' => 'Geangos', 'prenume' => 'Bogdan', 'sex' => 'm', 'varsta' => 23),
'e5' => array('nume' => 'Vasile', 'prenume' => 'Mihai', 'sex' => 'm', 'varsta' => 25)
);
$obj = new stdClass();
foreach ($clasa as $key => $value) {
$obj -> $key = $value;
}
class Clasa {
function filtru($x, $a, $b) {
foreach($x as $elev => $arr) {
if($arr[$a] == $b) {
echo $arr['nume'].' '.$arr['prenume'].' '.$arr['varsta'].'<br/>';
}//if end
}//foreach end
}//function end
public function __construct(filtru($x, $a, $b)) {
$this -> lista = $x;
$this -> cheie = $a;
$this -> valori = $b;
}
}//class end
$z = new Clasa($clasa, 'sex', 'm');
echo $z;
?>