在函数内部调用Object

时间:2022-04-10 21:33:24

So I'm not to OOP in PHP.

所以我不是在PHP中使用OOP。

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.

这是我的问题我有一个对象,我可以调用一个函数,它提供了一个arrary。所以这是代码。

$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

致命错误:在非对象上调用成员函数grabArray()

1 个解决方案

#1


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)

这不是OOP问题,而是范围问题。在函数go()中看不到$ obj。您需要将其作为参数传递,或者使用global关键字将其带入函数的作用域(不推荐)

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.

还有另一个类似于OOP组合概念的解决方案 - 你可以让go()函数负责创建一个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

但这可能并不理想,因为每次执行go()时都会创建一个全新的OBJ实例。您可以通过使用静态变量“缓存”go()中的OBJ实例来解决此问题

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.

但是这种类似组合的方法实际上只有在go()函数内部不使用OBJ实例时才有用 - 如果你在其他地方使用它,那么参数方法是最好的选择。

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

这就是为手头的任务选择正确的解决方案!

#1


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)

这不是OOP问题,而是范围问题。在函数go()中看不到$ obj。您需要将其作为参数传递,或者使用global关键字将其带入函数的作用域(不推荐)

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.

还有另一个类似于OOP组合概念的解决方案 - 你可以让go()函数负责创建一个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

但这可能并不理想,因为每次执行go()时都会创建一个全新的OBJ实例。您可以通过使用静态变量“缓存”go()中的OBJ实例来解决此问题

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.

但是这种类似组合的方法实际上只有在go()函数内部不使用OBJ实例时才有用 - 如果你在其他地方使用它,那么参数方法是最好的选择。

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

这就是为手头的任务选择正确的解决方案!