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 14 of 14
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OOP/Class Question

    Hi all,

    I am an fairly experienced PHP programmer - but I am used to procedural code - Currently I am refactoring to OOP.

    My question is as follows:

    I have 2 classes - a progressbar class and data fetcher class.

    In this implementation, the data fetcher need to directly access the progressbar.

    I could use extends, but then i cant use the datafetcher and progressbar classes independantly.

    What is the best way for me to achieve this?

    Thanks
    Fatman

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    If classes have a direct relationship, than inheritance should be used in this case.

    In other words, you should check what is referred to as "is a", or "has a" relationship. The "is a" relationship tells us that a class should inherit from a parent class, where as the "has a" relationship tells us that a class is composed of another class.

    For example, say that we have two classes. Our first class is called "House" and our second class is called "Door". to establish the relationship between those classes, you ask 2 questions.
    1- IS A house a door?
    2- Does a house HAS A door?

    You would agree that the second question is applicable in this case, and therefore, we apply composition (a class is composed of another class). However, if we examine the relationship between a "Person" and an "Employee", than we will find that an "Employee" IS A "Person" and in this case, the Employee class should inherit from the Person class.

    I hope that answers your question. I wrote a quick answer as I am having my lunch break and haven't got much time.

    Cheers,
    Ess

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    My assumption would be:

    A DataFetcher uses a ProgressBar, but it is not an evolution of ProgressBar. Hence they are seperate classes that do not inherit.

    The best way is to have a "progressBar" variable inside of the DataFetcher class, assign it in the constructor and then use it with $this->progressBar.

    PHP Code:
    class ProgressBar {
        public 
    $value;
    }

    class 
    DataFetcher {
        private 
    $progressBar;
        
        function 
    __construct() {
            
    $this->progressBar = new ProgressBar();
            
    $this->progressBar->value 0;
        }


  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    aedrin +1

    With an Interpreted language like PHP its important to keep inheritance structures as lean as possible (they are all loaded at runtime!), JAVA like inheritance is fine for a compiled (nearly) language like JAVA but has performance issues in PHP, so only use inheritance if there is a compelling need or a very close relationship.

    e.g. yes a house has a door, but so does a plane, submarine tree house etc so a door is probably best aggregated as per aedrin's example.

    e.g+ cars also have doors, so does your door class also subclass car ?, without multiple inheritance, (which php does not possess) it can't, so we are into the realm of duplicated code.

    I can however imagine a 'door' superclass that had subclasses for different types of door.

    PHP Code:
    <?php
    class car{
     function 
    car(){
      
    $this->door=new door();//or new car_door which extends door!
     
    }
    }

    class 
    house{
     function 
    house(){
      
    $this->door=new door('house');//else pass some data for lazy loading within door
     
    }
    }
    ?>
    etc
    Last edited by firepages; 03-21-2007 at 02:15 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thansk for the replies

    I got it now :
    PHP Code:
    class ProgressBar 
        public 
    $value


    class 
    DataFetcher 
        private 
    $progressBar
         
        function 
    __construct() { 
            
    $this->progressBar = new ProgressBar(); 
            
    $this->progressBar->value 0
        } 

    How do i access progressBars methods?

    $DataFetcher->progressBar->set(X) ??

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    That would only work if you make it public instead of private.

    Or you could add a method called getProgressBar() which returns the instance of the progress bar that it contains.

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well im on php4 which i beleive doesnt support private public declarations

    I replaced the public in the example with a "var"

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    well im on php4 which i beleive doesnt support private public declarations
    I'd recommend switching to PHP5. You're just hurting yourself (and us ) in the process.

  • #9
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    if things other than the DataFetcher need to be able to talk to the ProgressBar, then it maybe shouldn't be structured as you have it. If instead, they just need to tell the DataFetcher something, then let them do that, and let the DataFetcher worry about passing the required bits on to the ProgressBar.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #10
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok well refernce the variable (as a private) works from the constructor using the code below:

    $this->prb->moveStep(Blah)

    But from a class member function, i get :

    Fatal error: Call to a member function on a non-object

    What am i doing wrong?

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Somewhere the variable $this->prb is getting modified to not be a class/object, or the progressBar somehow got rid of itself.

    It depends on your code.

  • #12
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    Somewhere the variable $this->prb is getting modified to not be a class/object, or the progressBar somehow got rid of itself.

    It depends on your code.
    Ok here is the isolated code:

    testfile.php

    PHP Code:
    <?
    include("testclass.php");

    $test = new testing();

    $test->movebar(40);
    ?>
    Class:

    PHP Code:
    <?

    class testing{

        var 
    $prbar;

        function 
    __construct() {  

            
    $this->time=time();
                
    $this->prbar = new ProgressBar(30020); 

        }

        function 
    movebar($distance){

            
    $this->prbar->moveStep($distance);
        
        }

    }
    ?>
    Still getting:

    Fatal error: Call to a member function on a non-object in /home/fatman/public_html/testclass.php on line 15

  • #13
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    if you're using PHP4 then __construct won't be called. Constructors before 5 take the name of the class, so
    PHP Code:
    function testing() {
      
    $this->prbar = new Progressbar();

    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #14
    Regular Coder
    Join Date
    Apr 2003
    Posts
    103
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah Thanks!

    I checked out your blog too GJay


  •  

    Posting Permissions

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