PHP : call variable from another function in class

后端 未结 5 992
死守一世寂寞
死守一世寂寞 2021-01-24 21:14

This is my class code:

class myClass
{

   public function myFunc()
   {
      $myvar   =  \'Test str\';
   }

   public function result()
   {
      echo myClas         


        
5条回答
  •  醉话见心
    2021-01-24 21:40

    class myClass {
        public $myvar;
        public function myFunc() {
            $this->myvar = 'Test str';
            return $this;
        }
    
        public function result() {
            echo $this->myFunc()->myvar;
        }
    }
    
    $nCls = new myClass;
    $nCls->result();
    

    You can do this but this is not a good practice.

提交回复
热议问题