SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a function in specific include

    I'd like to be able to call a function in a specific include file. Is there any way to do this? The name of the include file will also be dynamic.

    If this isn't possible, is it possible to call a function by it's name (stored in a string), such as CallByName in Visual Basic?

    I'm new to PHP, but have been programming in ASP for several years. My web server is running PHP5.

    Thank you for any assistance.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    An include basically merges the two files so to call the function you just call it with no specific references. If it's included, it doesn't matter what file it is in. If you have a function name as a string, you can call it like this:
    PHP Code:
    $func="myfunction";
    $func($param1,$param2); 
    Saul

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And you can also include files using a variable if you ned to, for example.
    require_once $thefile;

  4. #4
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that worked!

    I need this because I'm going to be adding many includes with dynamic names, and each include will have functions with the same names (that would be the preferred method). But since I cannot use namespaces, I've had to resort to prefixing the names of the functions in each include with the name of the include, and having to call that function using the method above.

    Is this the best way to do this, or is there some other method that is somewhat more... elegant?

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, you could use OOP. A lot of classes implementing the same interface. It would not only be elegant but more efficient too.
    Saul

  6. #6
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, but my OOP knowledge only extends to a few months worth of crash-course C++.

    So... would I create an abstract class with the interface defined, then create a derived class in each include that implements the interface? How would I create objects of this class if the class name is dynamic (based on the name of the include file)? I know that these things can be accomplished through some form of polymorphism, but I'm a little foggy on the details.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, exactly, you got it all right. When your derived classes have methods with the same parameter list, that is polymorphism, and that's what you want. And you can dynamically create an object just as you dynamically call the function:
    PHP Code:
    $class="myclass";
    $myobj=new $class(); 
    Saul

  8. #8
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all your help, this will probably save me hours of headaches. I'm definitely beginning to like PHP more and more...

    Thanks again!

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You're welcome
    Saul


Bookmarks

Posting Permissions

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