www.webdeveloper.com
Results 1 to 2 of 2

Thread: OOP problem w/ ::PARENT

  1. #1
    Join Date
    Oct 2003
    Posts
    215

    OOP problem w/ ::PARENT

    Is there a way to couple 'PARENT' to the specific class, such as $this->PARENT::Function();

    example:
    Object B extends Object A
    Object C accesses Object B through $GLOBAL

    Object B's PARENT::FUNCTION() now tries to access Object C's parent - whereas I want it to access object B's parent.
    Last edited by Dudsmack; 02-11-2008 at 01:26 PM.
    #define TRUE FALSE
    //Happy debugging suckers

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    This works:
    PHP Code:
    <?php
    class A
    {
       public function 
    example()
       {
          echo 
    "This is class A.";
       }
    }

    class 
    extends A
    {
       public function 
    example()
       {
          
    parent::example();
       }
    }

    class 
    C
    {
       public function 
    example()
       {
          
    $GLOBALS['b']->example();
       }
    }

    $GLOBALS['b'] = new B();
    $c = new C();
    $c->example(); // outputs "This is class A."
    ?>
    However, it is rather contrary to the philosophy and goals of OOP, in that depending on a global value creates a strong coupling between objects, when loose coupling should be your goal.

    Perhaps you should look into making class B a singleton pattern, then get an instance of it within class C?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles