我想在php中找到使用面向对象的正确方法[关闭]

why this code does not print the value.

class Test{
    var $i;
    function Test($i){
        $this->i=$i;
    }

    function func1(){
        echo $i;
    }
}

$ob1=new Test(4);
$ob1->func1();  
?>

Here I am using object oriented concept

Replace

function func1(){
    echo $i;
}

with

function func1(){
    echo $this->i;
}

and will work fine try to learn using http://php.net/manual/en/language.oop5.php http://www.tutorialspoint.com/php/php_object_oriented.htm

You should echo $this->i not $i

function func1() {
   echo $this->i;
}

Output

4

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

See HERE

So use

function func1(){
        echo $this->i;
    }