Search code examples
phpfunctionclassconstructormember

PHP - accessing a non-class function inside a class


My Try: There is $getSomeData function in a file called bradpitt.php. Its a simple function. Which is not inside a class. Where I have another file name jolie.php. This file is having a class. Where I am trying to access $getSomeData()in that file.

CoolPlugin.php

class CoolPlugin extends plugin
{
    const COOLLIST = 'properties/coolBoy.json';

    public function getSomeData () {
        return DataUtil::readDataFile(self::COOLLIST);
    }

bradpitt.php (Non Class File - a simple function)

$getSomeData = function(){
    $plugin = new \simulator\CoolPlugin();
    return $plugin->getSomeData();
};

jolie.php

include_once 'bradpitt.php';
class Jolie{
    public $getSomeData;
    public function __construct(){
        global $getSomeData;
        $this->$getSomeData();
    }
}

output.php

include_once 'jolie.php';
$joiliePage = new Jolie();
var_dump($joiliePage->getSomeData);

ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**

How to invoke and access a simple function (having a return as an object) inside another class in PHP?

What I doing wrong where it returns NULL?


Solution

  • The code you posted is full of issues.

    var_dump($joiliePage->getSomeData);
    
    ERROR:
    Notice: Undefined variable: joiliePage in output.php on line 173
    Notice: Trying to get property of non-object in output.php on line 173
    **NULL**
    

    Assuming is line 173 is the one listed above, both error messages tell the same thing: the variable $joiliePage was not initialized (and the interpreter considers its value is NULL).

    Don't get fooled by the fact that PHP classifies them as "Notices". They are notices from the interpreter's point of view (it cannot find a variable) but they are errors for your code as it cannot continue successfully.


    include_once 'bradpitt.php';
    class Jolie{
        public $getSomeData;
        public function __contruct(){
            global $getSomeData;
            $this->$getSomeData()
        }
    }
    

    The function is called __contruct() but you probably want it to be the class' constructor. It is not the constructor and it is not called automatically by the interpreter because it doesn't have the correct name. The name of the constructor is __construct(). Notice there is an "s" in the middle that is missing in your code.

    The method __contruct() declares the global variable $getSomeData. If the file bradpitt.php is successfully included (it may fail with a warning without breaking the script if the file does not exists) then the $getSomeData symbol refers to the variable with the same name defined in file bradpitt.php.

    However, the call $this->$getSomeData() doesn't refer to this global variable. It uses the class' property with the same name, which is initialized. This code won't run.

    In order to call the function stored in the global variable $getSomeData, the code should read:

    public function __construct(){
        global $getSomeData;
        $getSomeData();
    }
    

    Also notice that the statement is missing a semicolon at the end and produces a syntax error. Your class' definition is incorrect, it doesn't compile and objects of type Jolie cannot be created.