在控制器中,我可以轻松调用这样的函数
$this -> user->login('username','password');
但是在单独的PHP文件框架之外,我该如何访问该方法?我试过这样的东西,但它不起作用:
include($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.PHP'); $userobj=new User(); $userobj->login('username','password');
你能帮忙吗?
<?PHP // Error Reporting error_reporting(E_ALL); // Check Version if (version_compare(PHPversion(),'5.1.0','<') == true) { exit('PHP5.1+ required'); } // Register Globals if (ini_get('register_globals')) { ini_set('session.use_cookies','On'); ini_set('session.use_trans_sid','Off'); session_set_cookie_params(0,'/'); session_start(); $globals = array($_REQUEST,$_SESSION,$_SERVER,$_FILES); foreach ($globals as $global) { foreach(array_keys($global) as $key) { unset(${$key}); } } } // Magic Quotes Fix if (ini_get('magic_quotes_gpc')) { function clean($data) { if (is_array($data)) { foreach ($data as $key => $value) { $data[clean($key)] = clean($value); } } else { $data = stripslashes($data); } return $data; } $_GET = clean($_GET); $_POST = clean($_POST); $_REQUEST = clean($_REQUEST); $_COOKIE = clean($_COOKIE); } if (!ini_get('date.timezone')) { date_default_timezone_set('UTC'); } // Windows IIS Compatibility if (!isset($_SERVER['DOCUMENT_ROOT'])) { if (isset($_SERVER['SCRIPT_FILENAME'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\','/',substr($_SERVER['SCRIPT_FILENAME'],0 - strlen($_SERVER['PHP_SELF']))); } } if (!isset($_SERVER['DOCUMENT_ROOT'])) { if (isset($_SERVER['PATH_TRANSLATED'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\',substr(str_replace('\\\\','\\',$_SERVER['PATH_TRANSLATED']),0 - strlen($_SERVER['PHP_SELF']))); } } if (!isset($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1); if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; } } if (!isset($_SERVER['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = getenv('HTTP_HOST'); } // Helper require_once(DIR_SYSTEM . 'helper/json.PHP'); require_once(DIR_SYSTEM . 'helper/utf8.PHP'); // Engine require_once(DIR_SYSTEM . 'engine/action.PHP'); require_once(DIR_SYSTEM . 'engine/controller.PHP'); require_once(DIR_SYSTEM . 'engine/front.PHP'); require_once(DIR_SYSTEM . 'engine/loader.PHP'); require_once(DIR_SYSTEM . 'engine/model.PHP'); require_once(DIR_SYSTEM . 'engine/registry.PHP'); // Common require_once(DIR_SYSTEM . 'library/cache.PHP'); require_once(DIR_SYSTEM . 'library/url.PHP'); require_once(DIR_SYSTEM . 'library/config.PHP'); require_once(DIR_SYSTEM . 'library/db.PHP'); require_once(DIR_SYSTEM . 'library/document.PHP'); require_once(DIR_SYSTEM . 'library/encryption.PHP'); require_once(DIR_SYSTEM . 'library/image.PHP'); require_once(DIR_SYSTEM . 'library/language.PHP'); require_once(DIR_SYSTEM . 'library/log.PHP'); require_once(DIR_SYSTEM . 'library/mail.PHP'); require_once(DIR_SYSTEM . 'library/pagination.PHP'); require_once(DIR_SYSTEM . 'library/request.PHP'); require_once(DIR_SYSTEM . 'library/response.PHP'); require_once(DIR_SYSTEM . 'library/session.PHP'); require_once(DIR_SYSTEM . 'library/template.PHP'); require_once(DIR_SYSTEM . 'library/openbay.PHP'); require_once(DIR_SYSTEM . 'library/ebay.PHP'); require_once(DIR_SYSTEM . 'library/amazon.PHP'); require_once(DIR_SYSTEM . 'library/amazonus.PHP'); ?>
而不是这样做:
原文链接:https://www.f2er.com/php/240049.htmlinclude($_SERVER['DOCUMENT_ROOT'].'/mydir/opencart/system/library/user.PHP');
(包括仅用户类),包括将从OpenCart加载所有系统类的startup.PHP(可能不需要或可能与您需要使用这些OpenCart类的项目范围发生冲突,但这可以在问题时修复发生).您还需要包含OpenCart的config.PHP,以确保OpenCart的DB类具有连接到已定义的DB所需的常量.您可能希望通过检查文件是否首先存在来以明确的方式执行此操作:
$root = $_SERVER['DOCUMENT_ROOT'] . '/mydir/opencart/'; if (file_exists($root . 'config.PHP')) { require_once($root . 'config.PHP'); } if (file_exists($root . 'system/startup.PHP')) { require_once($root . 'system/startup.PHP'); } if (file_exists($root . 'system/library/user.PHP')) { require_once($root . 'system/library/user.PHP'); }
$user = new User(); if ($user->login('username','password')) { echo 'User was logged in successfully'; } else { echo 'User not found or username or password do not match.'; }
这将是一个代码示例.
请看一下admin / index.PHP,在创建User对象之前如何实例化所有必需的类.
$registry = new Registry(); $loader = new Loader($registry); $registry->set('load',$loader); $config = new Config(); $registry->set('config',$config); $db = new DB(DB_DRIVER,DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE); $registry->set('db',$db); // ... do the same for rest of required classes ... $user = new User($registry);