I am trying to get into php object oriented programming.

<?php
class myClass{

public $myVar="this is demo";

public function myTextdemo(){
        echo $myVar;
    }
}
$obj= new myClass;
echo $obj->$myVar;

?>

It says

Fatal error: Cannot access empty property on line 11

What's wrong with my code?

Change it like this:

<?php
class myClass
{
    public $myVar="this is demo";

    public function myTextdemo()
    {
        return $this->myVar; # the problem was here
    }
}

$obj = new myClass();
echo $obj->myVar; # and here you wrote $obj->$myVar with an extra $
?>

use return to output data from a function not echo and use $this inside the class functions, otherwise the declared property will not be considered and you will get an error like Undefined variable...

http://www.php.net/manual/en/language.oop5.properties.php

bye! :)

Awesome. Solved. THanks cereal :)

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.