Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Nested Classes?

  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested Classes?

    Hello, I was wondering if nested classes would work so they can be called like this:

    $class->subclass->subclass'smethod();
    and if subclass would be able to access class's variables like child functions?
    -Thanks.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,338
    Thanks
    13
    Thanked 348 Times in 344 Posts
    1) that's not a nested class
    2) access like that won't work

    in the expression $class->subclass->subclass_method(); it's just that the class's property subclass is an object itself. whether this object was passed down to class or was created inside class does not matter.

    ex.
    PHP Code:
    // bear in mind that this code is only for demonstration
    // since the class design is pretty bad
    class base
    {
        public 
    $subclass;
    }
    class 
    child
    {
        public function 
    someMethod()
        {
            
    // do something ...
        
    }
    }

    // create a new base class
    $class = new base;
    // create a new child class
    $child = new child;
    // attach child to base
    $class->subclass $child;
    // call subclass method
    $class->subclass->someMethod(); 
    design-wise it is a bad idea to make the subclass property public because public properties can be changed without control. let's assume the following:
    PHP Code:
    $class = new base;
    $class->subclass = new child;
    // ...
    $class->subclass "awesome comment";
    $class->subclass->someMethod(); 
    this code will fail with a fatal error since subclass is now a string and not an object.

    however, instead of a public property you can use setters and getters
    PHP Code:
    class base
    {
        private 
    $subclass;

        
    // define a setter. this code verifies via type hinting that $subclass
        // is actually a child instance
        
    public function setSubclass(child $subclass)
        {
            
    $this->subclass $subclass;
        }

        
    // and while we're at it, we define a getter as well
        
    public function getSubclass()
        {
            
    // error out if subclass has not been defined yet
            
    if (!($this->subclass instanceof child))
            {
                throw new 
    Exception("Subclass does not exist");
            }
            return 
    $this->subclass;
        }
    }

    // define base class
    $class = new base;
    // define subclass
    $child = new child;
    // pass child to base
    $base->setSubclass($child);
    // call child class
    // will throw an exception if subclass has not been set before
    $base->getSubclass()->someMethod(); 
    you could get even more generic. if you only need to call the someMethod() method out of class, then defining an interface loosens the coupling between base & child:
    PHP Code:
    interface DoSomething
    {
        public function 
    someMethod();
    }
    class 
    child implements DoSomething
    {
        public function 
    someMethod()
        {
            
    // do something ...
        
    }
    }
    class 
    enkel implements DoSomething
    {
        public function 
    someMethod()
        {
            
    // do something ...
        
    }
        public function 
    doSomething()
        {
            
    // do something else ...
        
    }
    }
    // now it doesn't matter if we're passing the child class or the enkel class
    // the code will work both ways

    class base
    {
        private 
    $subclass;

        
    // define a setter. this code verifies via type hinting that $subclass
        // HAS A someMethod() FUNCTION
        
    public function setSubclass(DoSomething $subclass)
        {
            
    $this->subclass $subclass;
        }

        
    // and while we're at it, we define a getter as well
        
    public function getSubclass()
        {
            
    // error out if subclass has not been defined yet
            // since we used an interface for type checking, wee need to use the same interface here
            
    if (!($this->subclass instanceof DoSomething))
            {
                throw new 
    Exception("Subclass does not exist");
            }
            return 
    $this->subclass;
        }

    Last edited by Dormilich; 01-21-2012 at 09:21 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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