在类中包含和/或包装非类的好模式是什么?例如,我需要使用这个 php 文件https://github.com/widop/phpbb3/blob/master/common.php来登录我的 phpbb 板。具体来说,我需要这个文件来引导加载 phpbb,然后我将使用 $user 和 $auth 变量来登录用户。在我的代码中,我有一个 AuthClient 类。
我正在尝试找出最佳实践来包含来自 phpbb 的 common.php 并在我的班级中使用它:
=========根据反馈编辑=======================
我相信我已经取得了进步,但它仍然无法正常工作。
收到错误:
[2013-09-05 14:28:49] log.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Cannot redeclare class auth' in /var/www/phpbb3/includes/auth.php:24
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
这引用了 bootstrap.php 加载的非命名空间类
https://github.com/widop/phpbb3/blob/master/common.php
https://github.com/widop/phpbb3/blob/master/includes/auth.php
引导程序.php
define('IN_PHPBB', true);
$phpbb_root_path = base_path() . "/phpbb3/";
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once(base_path() . '/phpbb3/common.php');
登录控制器.php - Laravel
use myproject\models\User;
use myproject\models\phpbb\Phpbb;
use myproject\models\phpbb\AuthClient;
use myproject\models\phpbb\User as PhpbbUser;
require_once(base_path() . '/app/models/Phpbb/bootstrap.php');
class LoginController extends BaseController{
public function login(){
//...login in main application
//Login in phpbb - more ewww
global $user;
global $auth;
$phpbb = new AuthClient($user, $auth);
$phpbb->login();
}
}
AuthClient.php
<?php
namespace myproject\models\phpbb;
use myproject\models\phpbb\Phpbb;
class AuthClient{
protected $user;
protected $auth;
public function __construct($user, $auth){
$this->user = $user;
$this->auth = $auth;
}
public function login($user_id, $admin, $autologin){
$this->user->session_begin();
$this->auth->acl($this->user->data);
$result = $this->user->session_create($user_id, $admin, $autologin, true);
}
public function logout(){
$this->user->session_kill();
$this->user->session_begin();
}
}
反馈之前的原始代码 - 不再使用**
class AuthClient implements IAuthClient{
protected $user;
protected $auth;
public function __construct(){
/** Bootloading PHPBB */
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$this->user = $user;
$this->auth = $auth;
$this->user->session_begin();
$this->auth->acl($user->data);
}
public function login($user_id, $admin, $autologin){
$result = $this->user->session_create($user_id, $admin, $autologin, true);
}
public function logout(){
$this->user->session_kill();
$this->user->session_begin();
}
}