在php类对象中不能正常工作吗?

时间:2015-05-10 17:05:15

标签: php

<?php 
class Door
{
    public function __construct()
    {

    }
    public function test(){
        echo "welocme";
    }
}
$obj=new Door();

get_data();

function get_data(){

$obj->test();

}

$ obj-&GT;测试();在功能之外工作,但我需要内部功能。我无法访问函数show error

中的对象
Fatal error: Call to a member function test()

1 个答案:

答案 0 :(得分:1)

尝试这样:它可能会有效..

如果你在函数中使用任何外部变量,则取消为global $use_variable_name。现在你可以理解......

function get_data(){
global $obj;
$obj->test();

}

另一种更好的方法:

get_data($obj);// call this way...
function get_data($object){
$object->test();

}