简体   繁体   English

如何在php中使用范围解析运算符访问非静态成员变量?

[英]how to access a non static member variable using scope resolution operator in php?

In php using :: we can access a non static member function , but how can i access the member variable same using scope resolution operator ? 在使用::的php中,我们可以访问一个非静态成员函数,但是如何使用作用域解析运算符访问相同的成员变量呢?

<?php 
class abc
{
    public static $data="i am static membervaribale".'</br>';

    public $data1="i am not a static membervaribale".'</br>';
    public   function a()
    {
        echo "I am a non static method".'</br>';
    }

    public  function getsize()
    {
        return self::$data;
    }
}

echo abc::$data;

//echo abc::$data1;//showing error;

echo abc::a();

echo abc::getsize();

$obj=new abc;
echo $obj->data1;


?>

the access to not static member the class is based on $this. 该类的非静态成员的访问基于$ this。 So you should use 所以你应该使用

 $this->data1;

and for a new object of the class abc() 对于类abc()的新对象

 $myObejct = new abc();

 $myObject->data1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM