PHP : call variable from another function in class

后端 未结 5 987
死守一世寂寞
死守一世寂寞 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.

    0 讨论(0)
  • 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;
       }
    }
    
    0 讨论(0)
  • 2021-01-24 21:26
    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";
        }
    
    }
    
    0 讨论(0)
  • 2021-01-24 21:34

    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;
       }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题