PHP : call variable from another function in class

后端 未结 5 994
死守一世寂寞
死守一世寂寞 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:26

    the problem is the scope, you can't call a variable within another function, define a property for the class and set it from a function then retrieve the property with result():

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

提交回复
热议问题