Question

I would like to create an object, but i'm a beginner at OOP, tried the code below and it returns the error code:

Parse error: syntax error, unexpected '(', expecting '&' or variable (T_VARIABLE) in /home/acosor/work/bpideeas/branches/testing/clasa/clasa.php on line 25

Basically I would want to insert a function inside a construct, can't really wrap my head around how to do it.

<?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;

?>
Was it helpful?

Solution

You don't include functions inside methods in OOP normally (_construct is a method, all functions are called methods in php OOP and variables in the class' root level are properties)

Instead you create another method and call it from one of it's brother methods:

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" is used to reference current instance's methods or properties, I see you already used it to set some properties, so you can use it to call other methods in the instance as well.

Check encapsulation on google to see what are the differences when you declare these Methods public/private/protected


Edit: how to echo $z as per OP's request

You can't echo $z cos $z is not like the variable's you've seen before, it's holding an Instance of the Class Clasa, that's why you can't echo it, you normally can only echo single values like Strings Integers etc... For example you will get an error if you try to echo an array as well.

What do we do then? You could echo the properties, like echo $z->lista. But this is considered a bad practice, look around Stackoverflow a bit for a detailed explanation. Instead let's build a method to output the info you want, and an alternative, a method that returns the values and you can echo them from the outside or do whatever you want.

Class Clasa{ ... //previous code

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

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

}

So you now could do:

$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

OTHER TIPS

Don't do

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

but use

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

instead.

replace

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

with

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

and

replace

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

with

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

Are you trying to do this..?

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

If you want to call filtru() in constructor, try

public function __construct($x, $a, $b){
  $this->filtru($x, $a, $b); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top