创建两个互相使用的类?

时间:2021-07-10 12:22:26

In a messaging project, I have two classes, number and message. The first class does the stuff about numbers and second one does the messages processes.

在消息传递项目中,我有两个类,数字和消息。第一类是关于数字的东西,第二类是消息处理。

number->recive() should call message->getPass(). then message->getPass should produce a password, and reply it to user using message->send().

number-> recive()应该调用message-> getPass()。然后message-> getPass应该生成一个密码,并使用message-> send()将其回复给用户。

and there are many situations like this that I want this class in that and that in this...

并且有很多这样的情况,我想要这个类,在这个......

I tried $this->number = new number() in message class's __constructor() and vice versa, but got an Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 65488 bytes).

我在消息类的__constructor()中尝试了$ this-> number = new number(),反之亦然,但得到了致命错误:允许的内存大小为33554432字节耗尽(尝试分配65488字节)。

I think the error reason is obvious, I'm causing an infinite loop of instantiating.

我认为错误的原因是显而易见的,我正在造成一个无限循环的实例化。

Is There Any Way to Have Two Classes Which Use Each Other? Whats the Right Way to Go?

有没有办法让两个相互使用的课程?什么是正确的方式?

Thanks

Edit 0: Thanks for super fast answers/comments!

编辑0:感谢超快的答案/评论!

Edit 1: I saw this question How to create two classes in C++ which use each other as data? I don't have any idea what exactly those asterisks mean, and if I can use it in php!

编辑1:我看到了这个问题如何在C ++中创建两个使用彼此作为数据的类?我不知道这些星号究竟是什么意思,如果我可以在php中使用它!

Edit 2: about the codes caused error, simply:

编辑2:关于代码导致的错误,简单地说:

test.php:

include_once './number.php';
$number  = new number();
$number->recive();

number.php:

include_once './message.php';
class number {

    public function __construct($numberId = NULL) {
        $this->message = new message();
        $this->pdo = new PDO("mysql:host=localhost;dbname=madb", "root", "root");
    }
    ...
}

message.php:

class message {

    protected $pdo, $rows, $sql, $number;

    public function __construct($messageId = NULL) {
        $this->number = new number();
        $this->pdo = new PDO("mysql:host=localhost;dbname=madb", "root", "root");
    }
    ...
}

Edid 3:

Some kind of solution maybe this:

某种解决方案可能是这样的:

Add a load method to each class:

为每个类添加一个加载方法:

public function load($className) {
    require_once $className . '.php';
    $this->{$className} = new $className();
}

so you should call $this->load('number') to load number class from number.php whenever I need it, and then use it in this way $this->number->numberMethod() .

所以你应该在需要的时候调用$ this-> load('number')从number.php加载数字类,然后以这种方式使用它$ this-> number-> numberMethod()。

2 个解决方案

#1


2  

I would advise you - as jeff said in a comment - to create a third class which uses them both.

我会建议你 - 正如杰夫在评论中所说 - 创建一个使用它们的第三类。

However, a quick solution for your problem:

但是,快速解决您的问题:

Message class:

private $number;

public function __construct() {
  $this->number = new Number($this);
}

Number class:

private $message;

public function __construct($msg) {
  $this->message = $msg;
}

#2


-1  

You could have one or both classes be singleton, which would prevent the need to construct either one of them more than once... add a static getInstance() method to both which either returns a previously constructed instance or a new one... look up "singleton pattern" and you'll see how this works.

你可以让一个或两个类成为单例,这样可以防止需要多次构造其中一个...向两者都添加一个静态getInstance()方法,它返回一个先前构造的实例或一个新实例...查看“单身模式”,你会看到它是如何工作的。

#1


2  

I would advise you - as jeff said in a comment - to create a third class which uses them both.

我会建议你 - 正如杰夫在评论中所说 - 创建一个使用它们的第三类。

However, a quick solution for your problem:

但是,快速解决您的问题:

Message class:

private $number;

public function __construct() {
  $this->number = new Number($this);
}

Number class:

private $message;

public function __construct($msg) {
  $this->message = $msg;
}

#2


-1  

You could have one or both classes be singleton, which would prevent the need to construct either one of them more than once... add a static getInstance() method to both which either returns a previously constructed instance or a new one... look up "singleton pattern" and you'll see how this works.

你可以让一个或两个类成为单例,这样可以防止需要多次构造其中一个...向两者都添加一个静态getInstance()方法,它返回一个先前构造的实例或一个新实例...查看“单身模式”,你会看到它是如何工作的。