我试图回显已传入类的变量。我有下面的代码(见下文)
engine.php
<?php
define('access', TRUE); //define to true so that we can access core.php
// engine
include_once('core.php'); //include core.php
$core = new core();
j_interpreter("login"); //call j_interpreter function
function j_interpreter($key){
//switch request base on the request key
switch($key) {
case "login" :
extract($_POST);
$core->j_login($username, $password);
break;
default :
echo "default";
} //end of switch
}
?>
core.php
<?php
if(!defined('access')) :
//foreign access then dont allow!
die('Direct access not permitted');
endif;
class core{
public function ___construct(){
//macro stuff here
}
public function j_login($username, $password){
echo $username . " " . $password;
}
}
我试图获得用户名和密码的帖子数据已经从引擎。php传递到core.php,但遗憾的是它给了我错误
注意:未定义变量:core在C:'wamp'www'proj'core'engine.php
致命错误:在C:'wamp'www'proj'core'engine.php中调用非对象的成员函数j_login()
任何想法?
函数有它的作用域,在您的例子中,$core
是在全局作用域中定义的。要在函数内部访问它,你需要像global $core;
function j_interpreter($key){
global $core;
//switch request base on the request key
switch($key) {
case "login" :
extract($_POST);
$core->j_login($username, $password);
break;
default :
echo "default";
} //end of switch
}
参见PHP手册中的变量作用域