PHP : call variable from another function in class

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

    You are mixing up quite a few concepts.

    First, you have to create a new object of class myClass:

    $nCls = new myClass();
    

    Then, you can call the member function (method) on that class:

    $nCls->result();
    

    In result(), you just call the other method using $this:

    public function result()
    {
       echo $this->myFunc();
    }
    

    Note though that this does nothing. The variable $myvar is local and not a class attribute. I advise you read up on object oriented programming, and object oriented PHP in particular.

提交回复
热议问题