Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate two classes which use each other?
    text
    copied!<p>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.</p> <p><code>number-&gt;recive()</code> should call <code>message-&gt;getPass()</code>. then <code>message-&gt;getPass</code> should produce a password, and reply it to user using <code>message-&gt;send()</code>.</p> <p>and there are many situations like this that I want this class in that and that in this...</p> <p>I tried <code>$this-&gt;number = new number()</code> in message class's <code>__constructor()</code> and vice versa, but got an Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 65488 bytes).</p> <p>I think the error reason is obvious, I'm causing an infinite loop of instantiating.</p> <p>Is There Any Way to Have Two Classes Which Use Each Other? Whats the Right Way to Go?</p> <p>Thanks</p> <p>Edit 0: Thanks for super fast answers/comments!</p> <p>Edit 1: I saw this question <a href="https://stackoverflow.com/questions/4964482/how-to-create-two-classes-in-c-which-use-each-other-as-data">How to create two classes in C++ which use each other as data?</a> I don't have any idea what exactly those asterisks mean, and if I can use it in php!</p> <p>Edit 2: about the codes caused error, simply:</p> <p>test.php:</p> <pre><code>include_once './number.php'; $number = new number(); $number-&gt;recive(); </code></pre> <p>number.php:</p> <pre><code>include_once './message.php'; class number { public function __construct($numberId = NULL) { $this-&gt;message = new message(); $this-&gt;pdo = new PDO("mysql:host=localhost;dbname=madb", "root", "root"); } ... } </code></pre> <p>message.php:</p> <pre><code>class message { protected $pdo, $rows, $sql, $number; public function __construct($messageId = NULL) { $this-&gt;number = new number(); $this-&gt;pdo = new PDO("mysql:host=localhost;dbname=madb", "root", "root"); } ... } </code></pre> <p>Edid 3:</p> <p><strong>Some kind of solution maybe this:</strong> </p> <p>Add a <code>load</code> method to each class:</p> <pre><code>public function load($className) { require_once $className . '.php'; $this-&gt;{$className} = new $className(); } </code></pre> <p>so you should call <code>$this-&gt;load('number')</code> to load number class from number.php whenever I need it, and then use it in this way <code>$this-&gt;number-&gt;numberMethod()</code> .</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload