www.webdeveloper.com
Results 1 to 9 of 9

Thread: Please help with Objects

  1. #1
    Join Date
    Apr 2010
    Posts
    227

    Please help with Objects

    I have two seperate main classes for different parts of a website.

    Both classes are used to make a single object each.

    Each of those objects utilizes a shared "sub"class to make lots of small objects that are used for easy storage and retrieval of data.

    To keep them clean and small these "sub"objects call back to the parent in when more complex methods need to be run.

    Problem is thus.

    How do I:
    refer back to the objects creator in order to access its methods?

  2. #2
    Join Date
    May 2012
    Posts
    17
    Assuming you have this
    PHP Code:
    class MyParent
    {
        public function 
    doSomething()
        {
            
    //do something
        
    }
    }

    class 
    MyChild extends myParent
    {
          public function 
    doSomething()
        {
               
    //do something slightly different in the child
              
    parent::doSomething(); //then do exactly what the parent did.
        
    }


    $myChild = new MyChild();
    $myChild->doSomething(); 

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    If it's not a parent:: issue as Alan suggested, then we may need a few more details about what actually needs to happen. Perhaps you'll need to pass $this as a parameter to the "small object", which it would then pass to its parent?
    PHP Code:
    <?php

    class TheParent
    {
        public function 
    test(iFoo $foo)
        {
            return 
    $foo->getText();
        }
    }

    class 
    TheChild extends TheParent
    {
        public 
    $name 'World';
    }

    interface 
    iFoo
    {
        public function 
    getText();
    }

    class 
    Foo implements iFoo
    {
        private 
    $obj;
        public function 
    getText()
        {
            return 
    "Hello, " $this->obj->name;
        }
        public function 
    createObject()
        {
            
    $this->obj = new TheChild();
        }
        public function 
    useObject()
        {
            echo 
    $this->obj->test($this);
        }
    }

    $foo = new Foo();
    $foo->createObject();
    $foo->useObject();
    Though this sort of circularity would make me think twice about this part of the application architecture.
    "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

  4. #4
    Join Date
    Apr 2010
    Posts
    227
    Yeah all to true nodog.

    If i have a really large class

    And a small class that extends the large class.

    What will that mean for system resources should i make hundreds of objects from the small class?

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    Hundreds of small objects from the same class probably does not really do anything significantly more processing-wise than creating the same number of arrays. If each object were derived from a different class and thus required 100 different class definition files to be require()'d, I might be a little worried, but even then that sort of performance issue is generally lost in the noise compared to data issues. While it never hurts to at least briefly consider performance issues at this level, the danger is spending a lot of time fiddling with it only to find the PHP parser/compiler does a better job of optimizing things than you do.
    "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

  6. #6
    Join Date
    Apr 2010
    Posts
    227
    Cool.

    In that case
    It seems like i was over compensating!


    But a more exact question.

    Is an object created from a class that extends from a parent going to use more memory and or system processor than that of a same class that doesnt extend?

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,609
    There is probably a little overhead, but it's nothing I've ever worried about; but if you ever get the sort of traffic load where it would actually matter, you can probably afford a 2nd server along with a load-balancer. (Hardware is cheap these days compared to paying programmers for weeks/months of debugging code that is poorly designed/written. )

    For that matter, PHP arrays are somewhat notorious for using excessive memory (at least in some people's view), so you may still be ahead of the curve by creating objects custom-designed for the specific data you want to store/manipulate -- again pointing to the fact that fiddling around with micro-optimizations can be tricky and sometimes counter-intuitive. (If you really need to worry about every micro-second for a missile guidance system or such, you probably shouldn't be using PHP anyway. )

    Ultimately, I'm generally in favor of designing/coding for maintainability, and worrying about optimization only if/when you find you need it (though you always have it sort of kicking around in the back of your brain to make sure you aren't doing anything really stupid).
    "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

  8. #8
    Join Date
    Apr 2010
    Posts
    227
    THankyou nodog.

    Appreciate the help.

    WHile i use OOP a LOT i keep to my comfort zones and ignore much of its features.

  9. #9
    Join Date
    May 2012
    Posts
    17
    Why? Reach out and you'll find your development practices and your code gets much better by using all the features of object orientated code.

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