如何在PHP中的__construct()中放置一个函数

时间:2013-10-11 07:24:28

标签: php function object construct

我想创建一个对象,但我是OOP的初学者,尝试下面的代码并返回错误代码:

解析错误:第25行/home/acosor/work/bpideeas/branches/testing/clasa/clasa.php中的语法错误,意外'(',期待'&amp;'或变量(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;

?>

5 个答案:

答案 0 :(得分:2)

你通常不在OOP中的方法中包含函数(_construct是一个方法,所有函数都被称为php OOP中的方法,而类'根级中的变量是属性)

相反,你创建另一个方法并从它的兄弟方法之一调用它:

Class Clasa{
    public 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($x, $a, $b) {
        $this->filtru($x, $a, $b); // this calls the filtru method
        $this -> lista = $x;
        $this -> cheie = $a;
        $this -> valori = $b;
    }   
}

$this”用于引用当前实例的方法或属性,我看到你已经用它来设置一些属性,所以你也可以用它来调用实例中的其他方法。

检查google上的封装以查看声明这些方法时的差异public / private / protected


编辑:如何根据OP的请求回显$ z

你无法回复$z cos $z与你之前见过的变量不一样,它持有 Class Clasa的实例 ,这就是为什么你不能回应它,你通常只能回显单个值,如字符串整数等...例如,如果你试图回应一个数组,你会得到一个错误。

那我们该怎么办?您可以回显这些属性,例如echo $z->lista。但这被认为是一种不好的做法,请仔细查看Stackoverflow的详细解释。相反,让我们构建一个方法来输出你想要的信息,另一种方法是返回值,你可以从外部回显它们或做你想做的任何事情。

Class Clasa {     ... //以前的代码

public function showResults(){
    echo $this->lista.", ";
    echo $this->cheie.", ";
    echo $this->valori;
}

public function returnResults(){
    return "$this->lista, $this->cheie, $this->valori";
}   

}

所以你现在可以这样做:

$z->showResults(); // this will automatically echo the results because there are echoes in the method;
echo $z->returnResults();//that method will return the string, so you can echo it or do whatever you want with it

答案 1 :(得分:1)

不要做

public function __construct(filtru($x, $a, $b))

但请使用

public function __construct($x, $a, $b)

代替。

答案 2 :(得分:0)

替换

 public function __construct(filtru($x, $a, $b))

 public function __construct($x, $a, $b)

替换

$z = new Clasa($clasa, 'sex', 'm');
echo $z;

$z = new Clasa($clasa, 'sex', 'm');
echo $z->filtru($clasa, 'sex', 'm');

答案 3 :(得分:0)

如果您想在构造函数中调用filtru(),请尝试

public function __construct($x, $a, $b){
  $this->filtru($x, $a, $b); 
}

答案 4 :(得分:0)

你想这样做吗??

public function __construct($x,$a,$b) {
            filtru($x, $a, $b);
            $this -> lista = $x;
            $this -> cheie = $a;
            $this -> valori = $b;
        }