Access class property from outside of class

非 Y 不嫁゛ 提交于 2020-12-26 11:01:49

问题


Suppose I had the following class:

class MyClass {
    public function Talk() {
        $Say = "Something";
        return $Say;
    }
}

I then started an instance of the class:

$Inst = new MyClass();

How can I now call $Say outside MyClass hence, for example, echo it on the document? For example, having something like:

$Said = "He" . $Say

回答1:


I strongly recommend you read through http://php.net/manual/en/language.oop5.php. It will teach you the fundamentals of OOP in PHP.


In your example, $Say is just another variable declared within Talk()'s scope. It is not a class property.

To make it a class property:

class MyClass {
    public $say = 'Something';

    public function Talk() {
        return $this->say;
    }
}

$inst = new MyClass();
$said = 'He ' . $inst->say;

That defeats the purpose of Talk() however.
The last line ought to be $said = 'He '. $inst->Talk();




回答2:


$say is not a class property. If it was, you would define your class like this:

class MyClass {
    public $say;      
}

It is instead a local variable of the function Talk(). If you want to access it the way that you have the class defined, you would do:

$instance = new MyClass();
$instance->Talk();



回答3:


You need to make the $Say an instant variable of the MyClass class.

class MyClass {
    public $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
}

You could then access the instance variable from outside the class via $Inst->Say

Additionally it is better practice to encapsulate your class instance variables and use a "getter" method to retrieve the values.

class MyClass {
    private $Say
    public function Talk() {
        $this->Say = "Something";
        return $this->Say;
    }
    public getSay() {
        return $this->Say;
    }
}

$Inst = new MyClass();
echo $Inst->getSay();



回答4:


The best practices of oop is NEVER have public properties in your class. The best way to manipulate properties of your class is to have separate methods that will return the value of the properties and set the value of the properties. So

class MyClass {
    private $say;

    // common setter, invoked when we trying to set properties as they are public
    public function __set($name, $value) {
        $methodname = 'set' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            $this->$methodname($value);
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // common getter, invoked when we trying to get properties as they are public
    public function __get($name) {
        $methodname = 'get' . ucfirst(strtolower($name));
        if (method_exists($this, $methodname)) {
            return $this->$methodname();
        } else {
            throw new Exception ('There is no "' . $name . '" property in "' . __CLASS__ . '" class');
        }
    }

    // setter for out private property $say, invoked by common setter when colling $a->say = "something";
    public function setSay($value) {
        $this->say = $value;
    }

    // getter for out private property $say, invoked by common getter when colling $a->say;
    public function getSay() {
        return $this->say;
    }

    public function Talk($monologue) {
        $this->say = (!empty($monologue))?$this->say:$monologue;
        return $this->say;
    }

}

So now you can access your private properties as they are public, and do all neccessary checks to not get bad values be stored in them. Like that:

$a = new MyClass;
$a->say = "Hello my friends !";
$a->Talk();
$a->Talk("Good bye !");
echo $a->say;

Or like that:

$a = new MyClass;
$a->setSay("Hello my friends !");
$a->Talk();
$a->Talk("Good bye !");
echo $a->getSay();

For more security, you can make setSay and getSay methods private, but then the second piece of code won't work.




回答5:


You'd need to declare that var before your functions, eg

class MyClass {
  public $say;
  function Talk() {
    $this->say = "something";
  }
}

and then

$Said = "He ".$Inst->$say;



回答6:


You can use

$said = "He" . $Inst->Talk();

in this case, or you can class

class MyClass {
var $say;
public function Talk() {
    $say = "Something";
    $this->say = $say;
    return $say;
}
}

and call

$said = "He" . $Inst->say;


来源:https://stackoverflow.com/questions/6749900/access-class-property-from-outside-of-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!