在PHP下面的这个小例子中,能够在用户类中创建变量然后能够在我创建用户对象的任何页面上使用它们的好方法是什么?
<?PHP
//user.class.php file
class User
{
function __construct()
{
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$MY_userid = $session->get('auto_id'); //user id number
$MY_name = $session->get('disp_name');
$MY_pic_url = $session->get('pic_url');
$MY_gender = $session->get('gender');
$MY_user_role = $session->get('user_role');
$MY_lat = $session->get('lat');
$MY_long = $session->get('long');
$MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
<?PHP
// index.php file
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/user.class.php';
$user = new User();
//how should I go about making the variables set in the user class available on any page where I initiate the user class?
// I know I can't use
// echo $MY_pic_url;
// 1 way I can think of is to have the class return an array but is there another way?
?>
答案 0 :(得分:3)
让他们成为公众成员:
class user
{
public $first_name;
function __construct()
{
$this->first_name = $_SESSION['first_name'];
}
}
$user = new user();
echo $user->first_name;
答案 1 :(得分:3)
详细说明兰斯的回答;如果类的重点只是数据的容器,而不是用数据做某事你是非常安全的。但OOP的一个好主要是坚持encapsulation。封装意味着,您可以从外部隐藏对象的内部细节,只允许外部通过它的接口方法访问字段。
假设您不希望从外部更改User对象中的字段,但只能访问它们,那么您最好使用以下内容:
class User
{
private $_userId;
// and a bunch of other fields
public function __construct( $data )
{
// do something with the data
}
public function getUserId()
{
return $this->_userId;
}
// and a bunch of other getters to access the data
}
说实话,你可以使用magic methods之类的__set和__get来模拟你想要的东西,并在__set方法中捕捉任何不想要的改变。
此外,我不会将会话用作全局变量。您应该将会话对象作为参数传递给它的构造函数(就像我在示例中所示)。这强制松散耦合。因为现在你的User对象被绑定到全局会话对象,但是将它传递给构造函数,任何数据都可以传入。这使得类更灵活。
修改强>
这是一个如何将对象(例如您的会话对象)传递给构造函数的示例。要记住的一件事是,会话对象的设计方式,它仍然在某种程度上强制紧密耦合,因为它要求通过get()方法获取属性。
class User
{
public function __construct( $data )
{
$this->_id = $data->get( 'id' );
$this->_firstname = $data->get( 'firstname' );
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
您可以使用一些选项来传播松散耦合,并使用户对象更灵活。
要求您为User对象提供的数据为:
不同的论点
class User
{
protected $_id;
protected $_firstname;
// etc;
public function __construct( $id, $firstname, /* etc */ )
{
$this->_id = $id;
$this->_firstname = $firstname;
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session->get( 'id' ), $session->get( 'firstname' ), /* etc */ );
<强>阵列强>
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should be an array
public function __construct( array $data )
{
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
// usage
$session = new YourSessionObject();
$array = /* fill this array with your session data */;
$user = new User( $array );
实施某些界面
// objects that implement this interface need to implement the toArray() method
interface Arrayable
{
public function toArray();
}
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should implement Arrayable interface
public function __construct( Arrayable $data )
{
// get the data by calling the toArray() method of the $data object
$data = $data->toArray();
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
class YourSessionObject implements Arrayable
{
public function toArray()
{
/* this method should return an array of it's data */
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
<强>等强>
还有其他一些选择,但这应该会给你一些想法。希望这会有所帮助。
答案 2 :(得分:2)
旁注:构造函数没有返回值,即return false
没有你想要的效果。
使用公共属性或受保护的属性+访问器方法 或者将$ session存储在对象中,然后将属性的每个查询“委托”到该$ session对象。
class User
{
protected $session;
function __construct($session)
{
$this->session = $session;
}
function get($name) {
if( ''==$this->session->get('auto_id')) {
throw new Exception('...');
}
return $this->session->get($name);
}
}
$user = new User($session);
echo $user->get('disp_name');
或使用“魔术”__get()方法,例如
class User
{
protected static $names = array(
'auto_id', 'disp_name', 'pic_url', 'gender',
'user_role', 'lat', 'long', 'newregister'
);
protected $properties = array();
function __construct()
{
global $session;
if($session->get('auto_id') != '') {
foreach(self::$names as $n) {
$this->properties[$n] = $session->get($n);
}
}
else {
throw new Exception('...');
}
}
function __get($name) {
return isset($this->properties[$name]) ? $this->properties[$name] : null;
}
}
$user = new User;
echo $user->disp_name;
答案 3 :(得分:1)
使用属性来存储它。
<?PHP
//user.class.php file
class User
{
public $MY_userid;
public $MY_name;
public $MY_pic_url;
public $MY_gender;
public $MY_user_role;
public $MY_lat;
public $MY_long;
public $MY_firstvisit;
function __construct()
{
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$this->MY_userid = $session->get('auto_id'); //user id number
$this->MY_name = $session->get('disp_name');
$this->MY_pic_url = $session->get('pic_url');
$this->MY_gender = $session->get('gender');
$this->MY_user_role = $session->get('user_role');
$this->MY_lat = $session->get('lat');
$this->MY_long = $session->get('long');
$this->MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
答案 4 :(得分:-1)
您最初也可以在$ _SESSION变量中保存用户对象。
答案 5 :(得分:-2)
<?PHP
//user.class.php file
class User
{
function __construct()
{
var $MY_userid;
var $MY_name;
var $MY_pic_url;
var $MY_gender;
var $MY_user_role;
var $MY_lat;
var $MY_long;
var $MY_firstvisit;
global $session;
if($session->get('auto_id') != ''){
//set user vars on every page load
$this->MY_userid = $session->get('auto_id'); //user id number
$this->MY_name = $session->get('disp_name');
$this->MY_pic_url = $session->get('pic_url');
$this->MY_gender = $session->get('gender');
$this->MY_user_role = $session->get('user_role');
$this->MY_lat = $session->get('lat');
$this->MY_long = $session->get('long');
$this->MY_firstvisit = $session->get('newregister');
}else{
return false;
}
}
}
?>
<?PHP
// index.php file
require_once $_SERVER['DOCUMENT_ROOT'].'/classes/user.class.php';
$user = new User();
print $user->MY_name;
?>