我在php中设计我的类时遇到了一些麻烦。 正如你在我的代码中看到的那样,我希望有一个Class实例,并且有更多的类作为孩子从一个到另一个“交谈”。即时通讯记录用户并将所有信息存储到变量中。在我的其他类中,我最近需要获取此UserData。 欢迎任何帮助和想法:)
class Factory
{
private $UserData;
public function Factory()
{
DB::connect();
$this->getLoggedUserData( $_SERVER['REMOTE_USER'] );
}
private function getLoggedUserData( $user )
{
$result = DB::query( "SELECT * FROM users WHERE user='$user' LIMIT 1" );
$this->UserData = $result->fetch_assoc();
}
public function getMyTasks()
{
// how to call that class, without instancing it over and over again
MyOtherClass -> getMyTasks();
}
}
class MyOtherClass
{
public function getMyTasks()
{
// how to access the "global" variable
$result = DB::query( "SELECT * FROM tasks WHERE userID=" . $UserData['userID'] . " LIMIT 1" );
// doSomething ($result);
}
}
class DB
{
private static $mysqli;
public static function connect()
{
$mysqli = new mysqli(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->conect_errno . ')' . $mysqli->connect_error);
}
mysqli_set_charset($mysqli, 'utf8');
self::$mysqli = $mysqli;
}
public static function query( $query )
{
$result = self::$mysqli->query( $query );
if ( self::$mysqli->error ) {
error_log("QUERY ERROR: " . self::$mysqli->error);
error_log("QUERY: " . $query);
}
return $result;
}
}
$Factory = new Factory();
答案 0 :(得分:1)
当我不了解您的申请规模等时,很难说什么对您最有利。
无论如何,最简单的方法就是这样:
$otherClass = new MyOtherClass();
$Factory = new Factory($otherClass);
班级工厂
class Factory
{
private $UserData;
private someClass;
public function Factory(&$someClass)
{
$this->someClass = $someClass;
DB::connect();
$this->getLoggedUserData( $_SERVER['REMOTE_USER'] );
}
...
<强>用法强>
$this->someClass->getMyTasks();
但是如果你只想访问父类的方法/变量,那么是扩展类。
答案 1 :(得分:1)
好的,这是解决问题的简单方法
请注意,这不完整。如果这是关于您期望的结果,请给我一些反馈
<?php
class Factory {
private $UserData;
private $UserTask;
public function Factory() {
DB::connect();
$this->getLoggedUserData($_SERVER['REMOTE_USER']);
}
private function getLoggedUserData($user) {
$result = DB::query('SELECT * FROM users WHERE user="'.$user.'" LIMIT 1');
$this->UserData = $result->fetch_assoc();
}
public function getMyTasks() {
// how to call that class, without instancing it over and over again
if (!isset($this->UserTask)) $this->UserTask = new MyOtherClass($this->UserData);
return $this->UserTask->getMyTasks();
}
}
class MyOtherClass {
private $UserData;
public function __construct($userData) {
$this->userData = $userData;
}
public function getMyTasks() {
// how to access the "global" variable
$task = DB::query('SELECT * FROM tasks WHERE userID='.$this->UserData['userID'].' LIMIT 1');
return $this->performTask($task);
}
public function performTask($task) {/* doSomething(); */}
}
// usage is not complete, waiting for some extra input
$factory = new Factory();
$taskResults = $factory->getMyTasks();
非常欢迎任何关于如何改进这一点的意见
让我们来看看如何解决代码中不同“应用”之间共享实例的问题
单身方法:在第一次调用时创建一个实例,所有后续调用都通过单个实例
注册表模式:在脚本开头创建的对象会获取所有已初始化的需求并存储它们。如果任何“app”需要基本的服务集(它不是独立的),那么将注册表 object
传递给它的初始化程序/构造函数。
我希望我能够很好地理解你的评论,如果没有随意提出并纠正我的话