0

I tried to run this code on xampp and also on codepad and it seems it is not compiling at all.

 <?php  

    class MyName{

    public $name="johnh";
    public $lastname="doe";

    public function setProperty($var_name,$var_lastname){

        $this->name=$var_name;
        $this->lastname=$var_lastname;

    }

    public function getProperty(){

        return $this->name. "   ". $this->lastname;


    } 
}

$obj=new MyName();

$obj->setProperty("jane","doe");
$obj->getProperty();

    ?>  

Is there anything wrong with the code?

4
Contributors
6
Replies
14
Views
4 Years
Discussion Span
Last Post by masterjiraya
0

how do u want an output if you are not trying to do it?
print? echo? var_dump()? print_r()?

echo $obj->getProperty();

1

Basically, you are not telling it to print something out. print_r, var_dump, echo etc. are used to output something to the browser. Change the last line to echo $obj->getProperty(); and that should print something out.

0

bops was right. because your just storing data to an array w/o knowing if it is stored to an array if you dont display the elements of the array by using array displaying functions such as print_r()

0

print_r() is different from echo(). print_r is from array while echo is from string. so to use a print or print_r function it needs an array. while echo is not. since you use class functions you need to use print or print_r function

print_r($obj->getProperty());

see what happens

Edited by masterjiraya

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.