1

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.

2
  • 1
    Learn more about static and non-static methods and vars php.net/manual/en/language.oop5.php Commented Nov 20, 2012 at 9:47
  • Also you need to learn Object oriented programming. Commented Nov 20, 2012 at 9:48

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

2
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.

Comments

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

Comments

1

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

Comments

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.