0

This is an outline of a problem which I'm struggling to solve in my code. I guess my knowledge of scope isn't that great.. I don't understand why the function getGreeting is giving a parse error.

<?php

class Class_1 {

    public $t;

    public function __construct() {
        $this->t = "hello world";
    }

    public function helloWorld() {
        return $this->t;
    }
}



$x = new Class_1();

function getGreeting() {
    return $x->helloWorld();;
}

echo getGreeting();

?>

the error I get is:

Fatal error: Call to a member function helloWorld() on a non-object.

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • Try `global $x;` juste before `return $x->helloWorld();` – PoulsQ May 20 '14 at 13:25
  • 2
    do NOT use global. You have to write your code like this: $function getGreeting($x) {} or create the instance of the class class_1 in the function like notulysses made in his answer. – Xatenev May 20 '14 at 13:25
  • 3
    @PoulsQ Please don't. There are other ways to cross scope boundaries. – deceze May 20 '14 at 13:25
  • 1
    Because your class was initialized outside class scope, so he can't access the class method's. – Marcelo Aymone May 20 '14 at 13:29
  • thank you. why can't you instantiate the object outside the function? Likewise, why would a global variable declared outside the function not work within the function? – Zach Smith May 20 '14 at 13:31
  • This has nothing to do with classes at all. It's simply about variable scope. – deceze May 20 '14 at 13:36
  • sorry i see the above has been answered – Zach Smith May 20 '14 at 13:36
  • @Xatenev @deceze : You're right, but the question was to have access to $x (which in this script is a global var, because it's defined with no scope), so I answered in a global way (I could say to use `$GLOBALS['x']` too). But you're right, my answer was not totally complete : NEVER USE GLOBAL VARS, it's heavy and slow down your app. – PoulsQ May 20 '14 at 13:41

1 Answers1

1

Because you need to initialize object in the function to access it's methods from it :

 function getGreeting() {
    $x = new Class_1();
    return $x->helloWorld();;
}

Example

potashin
  • 44,205
  • 11
  • 83
  • 107
  • thanks. so in order to access the class function in this way I'd have to instantiate an object every time? – Zach Smith May 20 '14 at 13:35
  • then I'm guessing that this is not considered good programming practice? it's actually for a abstraction class on mysqli. so I definitely don't want to create a new mysql connection every time i make a query... – Zach Smith May 20 '14 at 13:38
  • @Zach Smith : If you want a pure OOP structure then there should be no functions at all – only classes' methods. So you can add another class to extend mysqli class and in it's constructor initiate connection (for example). – potashin May 20 '14 at 13:44
  • thanks again :). last question. then for those purposes would it be ok stylistically speaking to make a procedural page that instantiated the class, and with a list of variables assigned to function calls from the class to get the data that you need for the whole app? – Zach Smith May 20 '14 at 13:55
  • @Zach Smith : Actually it is a very broad question, that depends on your app's structure, and as for OOP, there is special pattern `MVC` where M stands for `Model` class and which is used for business logic, for example, getting data from the database , so maybe you should look forward to reading about this things. – potashin May 20 '14 at 14:07
  • @Zach Smith : Glad to help you – potashin May 20 '14 at 21:39