PHP : call variable from another function in class

2019-03-01 13:38发布

问题:

This is my class code:

class myClass
{

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

   public function result()
   {
      echo myClass::myFunc()->$myvar;
   }
}

and I use this:

   $nCls = new myClass;
   $nCls->result();

To show Test str form myFunc() but nothing shown. I think the problem is :

echo myClass::myFunc()->$myvar;

Thanks for any help.

回答1:

The problem is that you declare $myvar only in the scope of method myFunc(). That means it is not visible outside that method. Declare it as a class member instead:

class myClass
{

   private $myvar;

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

   public function result()
   {
      echo myClass::myFunc()->$myvar;
   }
}


回答2:

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.



回答3:

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.



回答4:

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;
   }
}


回答5:

include "views.php";
class Controller extends views{

    function index(){
        $this->head();  
        $this->home();
    }
    function privacy(){
        $this->head();
        $this->privc();
    }
    function about(){
        $this->head();
        $this->abt();
    }
    function address(){
        $this->head();
        $this->add();
    }
}
$obj=new Controller();

if(isset($_GET['fun'])){
    $obj->$_GET['fun']();
}
else{
    $obj->index();  
}



   This is views.php code


   class views{
    function head(){
        include "views/header.php";
        echo "<br>this is header";  
    }
    function abt(){
        include "views/about.php";
        echo "<br>This is about us page";   
    }
    function home(){
        include "views/home.php";
        echo "<br>This is Home page";
    }
    function privc(){
        include "views/privacy.php";
        echo "<br>This is privacy page";
    }
    function add(){
        include "views/address.php";
        echo "<br>This is address page";
    }

}


标签: php class