4

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().

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).

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!

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!

Edit 2: about the codes caused error, simply:

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() .

9
  • 6
    Create a 3rd class that uses the other class maybe?
    – user2417483
    Aug 11, 2013 at 15:56
  • Why not using extends?
    – pregmatch
    Aug 11, 2013 at 15:57
  • @Webinan Don't you mean number->send() instead of message->send() ?
    – g4vroche
    Aug 11, 2013 at 16:10
  • @pregmatch then which one should extend the other? number class methods rely on message class methods, and message class methods rely on number class methods!
    – ptvty
    Aug 11, 2013 at 17:07
  • 1
    @Webinan See fabien.potencier.org/article/11/what-is-dependency-injection for a good explanation of Dependency Injection.
    – Gordon
    Aug 18, 2013 at 17:53

2 Answers 2

3

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;
}
5
  • Yes, a third class. In quick solution, what should be $msg? an instance of message class?
    – ptvty
    Aug 11, 2013 at 16:25
  • @Webinan If you get a fatal error it's your own fault for just copy / pasting without thinking.
    – Bart
    Aug 11, 2013 at 16:31
  • @Bart I know it's my fault, I'm not reporting a PHP bug! I copy/pasted nothing, I just wanted to know how should I have such a functionality, and this code was my approach.
    – ptvty
    Aug 11, 2013 at 16:48
  • @Webinan Yes, $msg is an instance of the Message class. Have you already tried my code? In test.php, you must instantiate a Message object, not a Number object! (@Bart: Did I miss any comments? Why do you comment on my answer? I'm just curious.)
    – ComFreek
    Aug 11, 2013 at 17:43
  • I just went with the 3rd class!
    – ptvty
    Aug 18, 2013 at 13:09
-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.

11
  • I looked up and used the singleton pattern for my PDO instance and in some other cases, thanks for suggestion.
    – ptvty
    Aug 18, 2013 at 13:12
  • Friends don't let use friends use Singletons /cc @Webinan
    – Gordon
    Aug 18, 2013 at 16:06
  • That's a terrible suggestion
    – Evert
    Aug 18, 2013 at 16:29
  • 1
    The right way is "don't use singletons, statics or globals" Avoid them like the plague. You can experiment with them once you got a better grasp on object oriented programming to figure out why they are bad, but for now just forget they exist.
    – Gordon
    Aug 18, 2013 at 17:58
  • 1
    @Webinan, no a singleton pattern implies that only one instance of a given class can exists, and effectively prevents subclassing / substition which is the whole reason anyone would want to use OOP in the first place.
    – Evert
    Aug 18, 2013 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.