1

I recently read an article on Wikipedia about Design Pattern

So far I've done this, but it returns Fatal error: Maximum function nesting level of '100' reached, aborting!

Logically I know, it will return nesting error, But, I don't understand how is the best step.

class Main {
    $this->Aa = new Aa;
    $this->Bb = new Bb;
    $this->Cc = new Cc;
    }
class Aa extends Main {
    function blahA() {
        // blah blah
        }
    function testA() {
        $ret[] = $this->blahA(); // Call inside class
        $ret[] = $this->Bb->blahB(); // Call outside class
        $ret[] = $this->Cc->blahC(); // Call outside class
        return $ret;
        }
    }
class Bb extends Main {
    function blahB() {
        // blah blah
        }
    function testB() {
        $ret[] = $this->blahB(); // Call inside class
        $ret[] = $this->Aa->blahA(); // Call outside class
        $ret[] = $this->Cc->blahC(); // Call outside class
        return $ret;
        }
    }
class Cc extends Main {
    function blahC() {
        // blah blah
        }
    function testC() {
        $ret[] = $this->blahC(); // Call inside class
        $ret[] = $this->Aa->blahA(); // Call outside class
        $ret[] = $this->Bb->blahB(); // Call outside class
        return $ret;
        }
    }

Basically i want to manage my classes design, in order the method in Aa class is also reusable in Bb class and vice versa. I curious, how to build the relationship like my classes above, How to extends the class to get above pattern? And what is the name of this pattern? please also give me a link that i can learn from.

Many Thanks,

hakre
  • 193,403
  • 52
  • 435
  • 836
Afrig Aminuddin
  • 772
  • 1
  • 9
  • 22
  • 13
    How in the world did reading about design patterns lead you to this monstrosity? I think you need to read http://en.wikipedia.org/wiki/Anti-pattern next. – chaos May 17 '12 at 17:09
  • 6
    What's the name of this pattern? I'm curious, really – Yago Riveiro May 17 '12 at 17:11
  • Use wikipedia only for first entry research. Then get the books an article is based on after you've found out it's the right topic. – hakre May 18 '12 at 12:07

1 Answers1

4

Consider creating Aa and Bb separately and using Dependency Injection so each class will have a reference to the other. You should make sure the two classes are not too tightly coupled though.

The Gang of Four (GoF) Design patterns book mentioned in the comments is a good one, but the Head First Design Patterns is a bit easier (also very enjoyable) for beginners.

Here is an example. Notice there are better ways to set a property in PHP, I put a setter function just to be explicit. Refer to this question for more info.

class Main {
    $this->Aa = new Aa;
    $this->Bb = new Bb;
    $this->Cc = new Cc;

    // Can use properties instead of setters
    $this->Aa->setB(Bb);
    $this->Aa->setC(Cc);

    $this->Bb->setA(Aa);
    $this->Bb->setC(Cc);

    $this->Bb->setA(Aa);
    $this->Bb->setC(Bb);
    }

class Aa {                // No need to extend Main, right?
    function blahA() {
        // blah blah
        }
    // Dependency injection, Bb is now a member of this class
    // Consider doing this with PHP properties instead
    // Using setter function to be more explicit
    function setB(Bb b) {
        this->Bb = b;
        }
    // Dependency injection, Cc is now a member of this class
    // Consider doing this with PHP properties instead
    // Using setter function to be more explicit
    function setC(Cc c) {
        this->Cc = c;
        }
    function testA() {
        $ret[] = $this->blahA(); // Call inside class
        $ret[] = $this->Bb->blahB(); // Call outside class
        $ret[] = $this->Cc->blahC(); // Call outside class
        return $ret;
        }
    }
// Class Bb ...
// Class Cc ...
Community
  • 1
  • 1
Brady
  • 10,207
  • 2
  • 20
  • 59
  • I agree. For something like this, Dependence Injection is what you would want. – EmmanuelG May 17 '12 at 17:14
  • 1
    by the way, further reading: http://en.wikipedia.org/wiki/Inversion_of_control. And read de [Gof](http://en.wikipedia.org/wiki/Design_Patterns) if you want to know about (real) patterns – Yago Riveiro May 17 '12 at 17:16
  • @Brady : thanks, would you mind to show me, an example code? :-) – Afrig Aminuddin May 17 '12 at 23:29
  • @naticap, Dependency Injection is a fancy way of saying using a setter on the objects. I'll add a little example. – Brady May 18 '12 at 06:01