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

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

get_data();

function get_data(){

$obj->test();

}

$obj->test(); work well outside function but i need inside function. I cannot access object inside function show error

Fatal error: Call to a member function test()

1 Answers1

1

Try like this: it may work..

If u use any outer variable in a function, then decleare as global $use_variable_name . now u can understand...

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

}

another and better way:

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

}
Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27