?
Solved

PHP: Using include/require inside classes

Posted on 2006-04-23
9
Medium Priority
?
370 Views
Last Modified: 2013-11-18
Howdy all,
  Is there a way to include()/require() a script of function definitions into a class?  Like this:

main.php
----
class a
{
    function a()
    {
        include('bstuff.php');
    }
}

bstuff.php
--------
function sayhello()
{
    echo 'hello';
}


$a = new a;
$a->sayhello();


Understand?  I'd like to keep uncommonly used functions in a separate file, and load them as needed into the class.

- Sean
0
Comment
Question by:headzoo
  • 3
  • 3
  • 3
9 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16519713
You can do that.

You can also use below example to load file from same folder as your class script resides in:

include dirname(__FILE__).'/bstuff.php';

-r-
0
 

Author Comment

by:headzoo
ID: 16520122
Roonaan,
Sorry, I probably didn't make myself clear, as I was getting ready to run out the door as I was typing the message.  Simply including a script, like in your example, doesn't work for what I'm hoping to do.  In your example, the function sayHello() ends up having global scope.  What I want is to include a script of function definitions that become class methods.  Here is another example:


mybigclass.php
-----------------------
class MyBigClass
{
      function loadHello()
      {
            include('hellofunctions.php');
      }
}


hellofunctions.php
-----------------------
function sayHello()
{
      echo 'Hello!';
}



myscript.php
-----------------------
include('mybigclass.php');

$myclass = new MyBigClass;
$myclass->loadHello();
$myclass->sayHello();

The last two lines are important.  When I call the loadHello() method, that will include hellofunctions.php, and then on the last line, $myclass->sayHello() shows how the sayHello() function defined in hellofunctions.php is now a method in MyBigClass.

Simply including a script at the top of the class script only puts the sayHello() function in the global scope.  It's not a class method.  And including it inside the loadHello() function also puts the sayHello() function into the global scope.  $myclass->sayHello() doesn't work.

- Sean
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16520282
No, then it is not possible,

You would have to create a class which extends the myBigClass:

class myNewClass extends myBigClass {
}

-r-
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:m1tk4
ID: 16521506
mybigclass.php
-----------------------

include('hellofunctions.php');
class MyBigClass extends MySmallClass
{
}


hellofunctions.php
-----------------------
class MySmallClass {
  function sayHello()
  {
     echo 'Hello!';
  }
}

myscript.php
-----------------------
include('mybigclass.php');

$myclass = new MyBigClass;
$myclass->sayHello();


===============================================

Any tricks like the one you are trying to do, and others (like trying to derive a class from different base classes based on a runtime value) are sometimes too confusing for PHP Accelerators.

I can't quite get why you would need to do what you are trying to do when you can just do a regular subclassing, maybe you can explain?






0
 

Author Comment

by:headzoo
ID: 16521548
m1tk4,
  The idea is I want to create a class that only "loads" methods as they are needed in that session.  A class might have 15 methods, but for that particular session, only a few will be used.  So why not keep them all in external scripts, and include them as needed.  Like this:

sayhi.php
-----------
<?php
function sayHi()
{
      echo 'Hi!';
}
?>


saybye.php
-----------
<?php
function sayBye()
{
      echo 'Bye!';
}
?>

main.php
------------
<?php

class Main
{
      
      function loadLib($lib)
      {
            include($lib . '.php');
      }
}

$main = new Main;
$main->loadLib('sayhi');
$main->sayHi();

In this example you can see that I have two different "libraries" of functions, in two different files.  I can load the libraries by calling Main::loadLib().

The class I'm developing will be used by other developers that may or may not need all of the methods in the class.  So the developers would use a loadLib() method to load only the method libraries that they'll be using.
0
 
LVL 15

Expert Comment

by:m1tk4
ID: 16522182
>>So why not keep them all in external scripts, and include them as needed.

Because it's going to be slower than having them all from the start.
0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 1200 total points
ID: 16522483
Can't you use empty methods then?

class myBigClass {
   function sayHello() { include 'functionality_for_sayHello.php';}
   function sayBye() { include 'functionality_for_sayBye.php';}
}

The includes will be done when you call the function.

-r-
0
 

Author Comment

by:headzoo
ID: 16524556
m1tk4,
  I think that depends on the number of methods.  The class I'm working on is a Flickr access class.  Flickr has like, 5 dozen different methods that can be called via XML-RPC.  I want to have them *all* available through the class, but it's likely any developer would only use 3 or 4 of them to accomplish a task.  Although I haven't run any profiles on the test, it seems it would be faster, and the code would be cleaner, to use include() 3 or 4 times then include 2000 lines of code right from the get go.

Roonaan,
  Yes, that's the method I'm looking at right now.  The class would have cached previous query results from Flickr, and if the cache is available, it returns that.  Otherwise it includes the correct "library" file to make the query, and then cache the results.

0
 
LVL 15

Accepted Solution

by:
m1tk4 earned 300 total points
ID: 16526357
>> and the code would be cleaner, to use include() 3 or 4 times then include 2000 lines of code right from the get go.

No, it's not, just because of the way PHP parsers, bytecode compilers and accelerators work. Most of the big PHP-based libraries are built as one huge file with classes - you may want to take a look at, say, nuSOAP, Smarty, etc. The only exclusion is probably ADOdb, but they have it splitted in files because there are a lot more developers involved and they are responsible for different parts of it.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question