Access class property from outside of class

前端 未结 6 437
感情败类
感情败类 2021-01-22 18:41

Suppose I had the following class:

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

I th

相关标签:
6条回答
  • 2021-01-22 19:03

    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();
    
    0 讨论(0)
  • 2021-01-22 19:08

    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.

    0 讨论(0)
  • 2021-01-22 19:10

    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;
    
    0 讨论(0)
  • 2021-01-22 19:17

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

    0 讨论(0)
  • 2021-01-22 19:17

    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;
    
    0 讨论(0)
  • 2021-01-22 19:18

    $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();
    
    0 讨论(0)
提交回复
热议问题