-2

I am pretty new to PHP and I have come accross a problem.

I have two files, index.php and test.php:

class Test {

        private $name;

        public function __construct($name) {
            $this->name = $name;
        }

        public final function getName() {
            return $this->name;
        }
      }

And

class Index {

        private $testVar;

        public function test() {
            return $this->testVar = new Test('test');
        }

        public function print() {
        echo $this->testVar->getName();
        }
      }

In Index.php, I would like to print the accessable function of Test.php, but that does not work like this. I get the following error:

Fatal error: Call to a member function getName() on a non-object in /home/Index.php on line 10

Line 10 being: echo $this->testVar->getName();

What am I doing wrong?

  • 2
    `echo $this->testVar->testFunction()` should work, if the class `Test` has an accessible function `testFunction`. How was it (the function) declared? Which errors does the line show? – Tobias Jun 21 '14 at 17:17
  • 2
    As a general tip, when mentioning errors in a question **always** paste the exact error message into your question. It may not make sense to you, but someone may be able to read it and explain exactly what went wrong. – IMSoP Jun 21 '14 at 17:26
  • @still_learning - it says "method 'testFunction()' not found in class". – user3763329 Jun 21 '14 at 17:44
  • Edited original post. – user3763329 Jun 21 '14 at 17:48
  • If you answered my question (*How was it (the function) declared?*) we might be able to help you. – Tobias Jun 21 '14 at 17:48
  • @still_learning - I have just edited the post. – user3763329 Jun 21 '14 at 17:53
  • It's not "thanks in advanced"!!! There is only one `d`!!!!! Argh!!!!! – Lightness Races in Orbit Jun 21 '14 at 17:54
  • The code you've posted for `class Index` is not valid PHP code. Please create a simple example of the code, test that actual example, and edit your question to show that example, and the **exact** error message you receive - all of it, not just the bit that you think is important as you wrote in the comment above. – IMSoP Jun 21 '14 at 19:15
  • Done. @IMSoP, Fatal error: Call to a member function getName() on a non-object in Index.php – user3763329 Jun 21 '14 at 19:39
  • @user3763329: He meant it that you outline this ordered and concise inside the question (not peu a peu in comments below). So that one could read the question from top to bottom, understand what you're asking about, where exactly the error is thrown and which line etc. and - very important - a self containing example, that is, just to execute right away on one's own computer. No data missing. No code missing. Quickly validating the error message is thrown at that place where you tell it is. As long as you don't understand the error message we also have a reference for those. Let me know. – hakre Jun 21 '14 at 20:13
  • @hakre just edited to hopefully, being understandable. – user3763329 Jun 21 '14 at 22:37
  • looks much better, but I think the order of the code went wrong, it's often better you write the file name again on top of each code chunk. anyway, the error message should give your the pointer what happened. Can you explain to yourself what the error means? Or is it just you understand the message but you have no clue why you get it? – hakre Jun 21 '14 at 22:47
  • @hakre, I do not know why I get this error. – user3763329 Jun 22 '14 at 10:45
  • You have called a method on a variable where the variable is not an object. But to call a method you need an object. That's the error is telling you. You need to ensure to have an object before you can call it's methods. Simple as that. I close against the reference question for such error messages, that should provide you more details. – hakre Jun 22 '14 at 11:05
  • The crucial point is that until you run ->test() your object hasn't been created, so trying to call ->print() will fail. The test() method is also a bit odd, because it creates an object, saves it to a private variable, and returns it, all in one line. – IMSoP Jun 22 '14 at 13:22

1 Answers1

0

In case you want to call testVar as $this->testVar->testFunction(); you can only use this way in some function in the class where you have private variable $testVar. In case you want to use $testVar outside the class you have to create getter as:

public function getTestVar() {
      return $this->testVar;
}

And after do something like this:

$myClass = new MyClass();
$myClass->test();
echo $myClass->getTestVar()->testFunction();
Facedown
  • 192
  • 1
  • 2
  • 10