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 9 of 9
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Class calling function in another class

    Alright... I have a function called getName() in a class called info.
    Then I have another function called getDropDown() in a class called adminFunctions. Now; I include these two classes into a normal php page called "category.php" like this:

    PHP Code:
    include("info.class.php");
    $info = new info;
    include(
    "admin.class.php");
    $theAdmin = new adminFunctions
    The question I have is how would I call the getName function in the info class from the getDropDown function in the adminFunctions class. I know when calling a function in the same class I would use $this, but I'm not sure about calling from a different class.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    New Coder
    Join Date
    May 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    //first require nessary class filse
    //assume your class files are in the same directory.
    require './info.class.php';
    require './admin.class.php';

    $info = new info;//creating object from the perticular class
    $theAdmin = new adminFunctions;

    //this is how you call the methods
    $name=$info->getName();
    $drpDown=$theAdmin->getDropDown() ;

    good luck...

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    I don't think that's quite what he's after.
    One option is to pass the first object to the second, either as part of the constructor, via some kind of purpose-built setter, or to the function that's going to use it, e.g. (the constructor approach)
    PHP Code:
    class AdminFunctions {
      function 
    AdminFunctions($info) {
        
    $this->info=$info;
      }

      function 
    getDropDown() {
        
    $this->info->someFunction();
      }
    }
    $info=new info;
    $admin=new AdminFunctions($info); 
    The second option is to use the 'global' keyword in function where you want the other class:
    PHP Code:
    class AdminFunction {
        ...
        function 
    getDropDown() {
             global 
    $info;
             
    $info->someFunction();
        }

    A this approach is to use the concept of a singleton, you'll have a globally accessible function called getInfo() that (using static variables) returns an 'info' object (after creating one if it doesn't exist), but I have to go now so no time for an example (this is a better approach than using globals though)

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    if the getName() does not require the instansiation of the class you can also make a static call ..

    $blah=info::getName();

    aggregation is my favourite approach ...

    PHP Code:
    <?php
    class AdminFunctions {
      var 
    $info;//external info object
      
    function AdminFunctions() {
        
    $this->info= new info();
      }

      function 
    getDropDown() {
        
    $this->info->someFunction();
      }
    }

    $admin=new AdminFunctions(); 
    ?>
    Last edited by firepages; 05-16-2006 at 04:27 PM.
    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
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    You wouldn't want to pass $info to the constructor, and then instantiate a new 'info' though, surely?

    From the classnames, i've got a feeling that the data-modelling isn't as good as it could be, and so choosing the best approach is difficult (if $info is some kind of widely-used utility, then the global/singleton approach is probably better, if it's just used within AdminFunction then passing it to the object is...)

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,106
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by GJay
    You wouldn't want to pass $info to the constructor, and then instantiate a new 'info' though, surely?
    lol no , typo = fixed cheers [/QUOTE]

    Quote Originally Posted by GJay
    From the classnames, i've got a feeling that the data-modelling isn't as good as it could be, and so choosing the best approach is difficult (if $info is some kind of widely-used utility, then the global/singleton approach is probably better, if it's just used within AdminFunction then passing it to the object is...)
    Deciding how to model such relationships does seem to have an inbuilt `errr` factor, I mostly only use singletons for DAO's or similar and not for anything that has to think too much... but then again I have at times regretted that as well ~ .. but I still work mostly with PHP4 so perhaps thats limiting .. then again I forsee even more complexities with abtracts and interfaces etc (even though I see the usefulness)
    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)

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    How about this method?
    PHP Code:
    <?php
    class foo
    {
        function 
    hello()
        {
            print 
    'Hello' "\n";
        }
    }

    class 
    bar
    {
        function 
    bye()
        {
            print 
    'Bye' "\n";
        }
    }

    class 
    family
    {
        function 
    __construct()
        {
            print 
    'Family' "\n";
        }
        static function 
    adopt()
        {
            
    runkit_class_adopt('family''foo');
            
    runkit_class_adopt('family''bar');
        }
    }

    family::adopt();
    $family = new family();

    $family->hello();
    $family->bye();
    /* Ouptuts:
    Family
    Hello
    Bye
    */
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    How about this method?
    PHP Code:
    <?php
    class foo
    {
        function 
    hello()
        {
            print 
    'Hello' "\n";
        }
    }

    class 
    bar
    {
        function 
    bye()
        {
            print 
    'Bye' "\n";
        }
    }

    class 
    family
    {
        function 
    __construct()
        {
            print 
    'Family' "\n";
        }
        static function 
    adopt()
        {
            
    runkit_class_adopt('family''foo');
            
    runkit_class_adopt('family''bar');
        }
    }

    family::adopt();
    $family = new family();

    $family->hello();
    $family->bye();
    /* Ouptuts:
    Family
    Hello
    Bye
    */
    ?>
    Multiple inheritance? I think I'd stick with composition
    Didn't know about runkit before though.

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Runkit can do some nice things... but I've yet to see a server with it enabled.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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