【发布时间】:2015-06-18 19:28:10
【问题描述】:
我有一种感觉,我忽略了一些简单的事情,但我不知道如何从我的类中的构造函数调用的函数中获取值。下面是一个非常简单的示例,但本质上,我需要在 index.php 页面上使用 userId 值,该值由构造函数调用的函数 getUser 返回。
提前感谢您的帮助!
index.php:
$test = new Test($username);
//Need to get value of userId here....
类/功能:
class Test
{
//CONSTRUCTOR
public function __construct($username)
{
$this->getUserId($username);
}
//GET USER ID
public function getUserId($username)
{
//DB query here to get id
return $userId;
}
}
我应该补充一点,我知道我可以初始化类,然后从 index.php 调用函数并以这种方式获取值。这是一个非常简单的示例,但我正在处理的一些脚本从构造函数中调用 6 或 7 个函数来执行各种任务。
【问题讨论】:
-
在
__construct做$this->id = $this->getUserId($username);然后在创建对象echo $test->id;之后
标签: php class constructor