PHP:无法将变量从类a传递到b
我研究并尝试了不同的方法,但我无法将变量从主类传递到子类。执行var_转储时始终显示NULL。这是因为我以数组形式发送吗 Userdata.class.phpPHP:无法将变量从类a传递到b,php,oop,Php,Oop,我研究并尝试了不同的方法,但我无法将变量从主类传递到子类。执行var_转储时始终显示NULL。这是因为我以数组形式发送吗 Userdata.class.php <?php /** * Class used for getting userdata */ class Userdata { public $userdata; function __construct() { } public function getUserData($username
<?php
/**
* Class used for getting userdata
*/
class Userdata
{
public $userdata;
function __construct()
{
}
public function getUserData($username, $password)
{
$this->userdata = array
(
'arr_username' => $username,
'arr_password' => $password
);
return $this->userdata;
}
}
session_handler.php
function formSubmitUser()
{
// Class the class Authentication
$userdata = new Userdata();
$userdata->authentication = new Authentication;
//First submit the form before processing the data
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$userdata->getUserData($username, $password);
$userdata->authentication->logIn();
}
}
function formSubmitUser()
{
$authenticator = new Authentication;
//First submit the form before processing the data
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$authenticator->getUserData($username, $password);
$authenticator->logIn();
}
}
如果我手动将值添加到主类构造中,一切似乎都正常。但如果我试图从函数getUserData获取它,我就无法使它工作
function __construct()
{
$this->userdata = "Sending data like this works";
}
我缺少什么吗?在类身份验证中,您没有从Userdata类调用父变量 这就是为什么从var_转储中得到NULL。在身份验证中,$userdata没有值。 尝试var_dumpparent::$userdata;
这将允许您访问它,因为您的代码下一步应该可以工作
function formSubmitUser()
{
// Class the class Authentication
$userdata = new Authentication;
//First submit the form before processing the data
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$userdata->getUserData($username, $password);
$userdata->logIn();
}
}
首先,将getter方法作为setter并不常见,我建议如果用户数据是数组,那么应该在构造函数中实例化它
function __construct()
{
$this->userdata = array();
}
public function setUserData($username, $password) {
$this->userdata['username'] = $username;
$this->userdata['password'] = $password;
}
public function getUserData() {
return $this->userdata;
}
您正在身份验证中重新声明userdata变量,它实际上覆盖了来自userdata类的变量。从身份验证类中删除var$userdata您应该遵循继承规则。您不需要创建这两个类的实例。 使用它而不是会话_handler.php
function formSubmitUser()
{
// Class the class Authentication
$userdata = new Userdata();
$userdata->authentication = new Authentication;
//First submit the form before processing the data
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$userdata->getUserData($username, $password);
$userdata->authentication->logIn();
}
}
function formSubmitUser()
{
$authenticator = new Authentication;
//First submit the form before processing the data
if (isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$authenticator->getUserData($username, $password);
$authenticator->logIn();
}
}
让我知道,如果你需要任何其他帮助我想你只是不明白如何使用OOP。这就是为什么我一直在尝试学习它,而不是什么都问。帮我找到正确的方向?我总是搜索我所有的问题,但我似乎找不到解决问题的方法。不要使用var,使用public或private这样简单的东西,但事实就是这样。非常感谢你。我发誓我记得在某种程度上人们使用了我的第一种方法$用户数据->身份验证->登录;谢谢你的提示。如果您不介意如何设置$authenticator->getUserData$username,$password$验证器->登录;进入$authenticate->setUserData$username,$password->logIn?在Userdata.class.php中添加以下方法。公共函数setUserData$username,$password{$this->userdata=array'arr\u username'=>$username,'arr\u password'=>$password;返回$this;}