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?

Recommended Answers

All 6 Replies

you used to forgot print_r function

When to use print_r()?

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

echo $obj->getProperty();

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.

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()

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.