[英]object not initiating as intended PHP OOP
I am new to php and OOP in general, so have decided to turn my existing site in an oo php one to gain some experience. 我是php和OOP的新手,因此决定将现有站点转换为oo php,以获取一些经验。 Here is the issue I am currently facing.
这是我目前面临的问题。
I have a class Normaluser which is extending from User. 我有一个从用户扩展的Normaluser类。 Inside the User class is a login method which returns an object containing an array of the Users class' properties.
User类内部是一个登录方法,该方法返回一个包含Users类属性数组的对象。
class NormalUser extends User {
public function __construct($loginarray){
$email = $loginarray['email'];
$pass = $loginarray['pass'];
var_dump ( parent::login($email,$pass) );
//return parent::login($email,$pass)
}
}
When doing a var_dump (as above) I get 当做一个var_dump(如上所述)我得到
bool(false) object(User)#8 (4) { ["user_id"]=> string(1) "1" ["first_name"]=> string(7) "Melanie" ["last_name"]=> string(6) "Janson" ["user_level"]=> string(1) "1" }
Yet after calling the code in my login.php page : 在我的login.php页面中调用代码后:
$postdata = User::mysqli_array_escape($_POST);
$email = $postdata['email'];
$pass = $postdata['pass'];
$userstart = new NormalUser($postdata);
var_dump($userstart);
I would think that calling $userstart
as a new NormalUser object would return the correct variables, yet when I do a var_dump (as written in above) I receive the following : 我认为将
$userstart
作为新的NormalUser对象调用将返回正确的变量,但是当我执行var_dump(如上所述)时,我收到以下信息:
object(NormalUser)#5 (4) { ["user_id"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["user_level"]=> NULL }
I do not understand where I have gone wrong, If anyone would be able to point me in the right direction I would be greatly appreciative. 我不明白我哪里出了问题,如果有人能够指出正确的方向,我将不胜感激。
Thank you. 谢谢。
EDITED : added user.php login method as requested 编辑:根据要求添加了user.php登录方法
public static function login($email, $pass) {
global $database;
$sql = "SELECT user_id, first_name, last_name, user_level FROM users WHERE (email='$email' AND pass=SHA1('$pass')) AND active IS NULL LIMIT 1";
$results = self::find_by_sql($sql);
//var_dump($results);
if (!empty($results)) {
return array_shift($results);
} else {
return false;
}
}
The problem is that you are returning an object in the constructor . 问题是您要在构造函数中返回一个对象 。 This is not how constructors work.
这不是构造函数的工作方式。
I have two options for you: 我有两种选择供您选择:
Either you create a static method that returns a NormalUser instance (instead of using the constructor): 您要么创建一个返回NormalUser实例的静态方法(而不是使用构造函数):
// In your NormalUser class. static function createNormalUser($loginarray) { $email = $loginarray['email']; $pass = $loginarray['pass']; return parent::login($email,$pass); }
Or you stick with the constructor but instead of returning an object instance, you assign its attributes to the $this
instance: 或者您坚持使用构造函数,而不是返回对象实例,而是将其属性分配给
$this
实例:
function __construct($loginarray) { $email = $loginarray['email']; $pass = $loginarray['pass']; $temp = parent::login($email,$pass); $this->user_id = $temp->user_id; // etc... }
new NormalUser()
creates a NormalUser
object, which is completely distinct from the User
object returned by User::login()
. new NormalUser()
创建一个NormalUser
对象,该对象与User::login()
返回的User
对象完全不同。 The User
object is immediately lost because you don't assign it to anything. User
对象立即丢失,因为您没有将其分配给任何对象。
One solution would be to assign each field manually: 一种解决方案是手动分配每个字段:
class NormalUser extends User {
public function __construct($loginarray){
$email = $loginarray['email'];
$pass = $loginarray['pass'];
if($base = parent::login($email,$pass)) {
$fields = array('user_id', 'first_name', 'last_name', 'user_level');
foreach($fields as $fld) {
$this->$fld = $base->$fld;
}
}
}
}
but that feels wrong to me. 但这对我来说是错的。 Another option would be:
另一种选择是:
$email = $postdata['email'];
$pass = $postdata['pass'];
$userstart = NormalUser::login($postdata);
var_dump($userstart);
If my guess about the behaviour of your find_by_sql
is correct, that should produce the result you want. 如果我对
find_by_sql
的行为的猜测是正确的,那应该会产生所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.