我是相当新的使用PHP类,我已经做了一些PHP使用程序方法一段时间,但从来没有完全完成适当的面向对象的PHP编程。这让我想到了我的问题:
我有一个名为classes.php的文件,我打算定义所有的php类。在那个文件里面我创建了一个类,如下所示:
require_once("connection.php");
class User
{
public function getUserInfo() {
$userinfo = mysqli_query($con,"SELECT * FROM users WHERE username='". $_SESSION['username'] ."'");
while($row = mysqli_fetch_assoc($userinfo)) {
// USER DETAILS:
$userid = $row['id'];
$username = $row['username'];
$userfirstname = $row['first_name'];
$userlastname = $row['last_name'];
$useremail = $row['email'];
$useraddress1 = $row['address_line_1'];
$useraddress2 = $row['address_line_2'];
$userpostcode = $row['postcode'];
$userphone = $row['phone'];
$usermobile = $row['mobile'];
$usercreated = $row['created'];
$usermodified = $row['modified'];
$useraccess = $row['access_level'];
}
}
} // end of User现在我想在另一个页面中使用该类中定义的变量的值。但我怎样才能访问这些值?
到目前为止,我正在尝试这个(失败):
include "php_includes/classes.php";
$user = new User();
?>
<?php echo $user->getUserInfo($username) ?>
include "php_includes/classes.php";
$user = new User();
?>
<?php echo $user->$username ?>
提前致谢。