Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an Object inside of a function

Tags:

function

oop

php

So I'm not to OOP in PHP.

Here is my issue I have a object that I can call a function from and it provides back an arrary. So here is the code.

$obj = new OBJ();

function go($url){
    $array = $obj->grabArray($url);
    echo $array['hits'];
}

go('http://www.mysite.com/hello');

This gives me the error

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

like image 652
Tim Avatar asked May 20 '09 19:05

Tim


People also ask

Can I create object inside function?

Function objects can also be created as part of an object literal. Below we create an object named circle with a property named area which is a function object. Next, let's look at an example where a function object is passed around like a regular object. The negate function takes a function as its argument.

How do you call an object in JavaScript?

The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

What is it called when you call a function inside a function?

Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function.

Can we call function inside function?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you call a function inside a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.


1 Answers

This is not an OOP issue, it's a scope issue. $obj isn't visible inside the function go(). You either need to pass it in as a parameter, or bring it into the function's scope with the global keyword (not recommended)

Recommended way

$obj = new OBJ();

go('http://www.mysite.com/hello', $obj);

function go( $url, $object )
{
    $array = $object->grabArray($url);
    echo $array['hits'];
}

Not Recommended way

$obj = new OBJ();

go('http://www.mysite.com/hello');

function go( $url )
{
    global $obj;
    $array = $object->grabArray($url);
    echo $array['hits'];
}

There's another solution which is similar to the OOP concept of composition - you would make the go() function responsible for creating an instance of OBJ.

go('http://www.mysite.com/hello');

function go( $url )
{
    $obj = new OBJ();
    $array = $obj->grabArray($url);
    echo $array['hits'];
}

This is probably not ideal, though, since you'd create a brand new OBJ instance every time you executed go(). You could fix this by "caching" the instance of OBJ inside go() with a static variable

function go( $url )
{
    static $obj;
    if ( is_null( $obj ) )
    {
        $obj = new OBJ();
    }
    $array = $obj->grabArray($url);
    echo $array['hits'];
}

But this composition-like approach is really only useful if you don't use your instance of OBJ anywhere else besides inside the go() function - if you do use it elsewhere, then the parameter approach is the best choice.

It's all about picking the right solution for the task at hand!

like image 96
Peter Bailey Avatar answered Oct 02 '22 07:10

Peter Bailey