PHP: Using include/require inside classes

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
headzooAsked:
Who is Participating?
 
m1tk4Commented:
>> 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
 
RoonaanCommented:
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
 
headzooAuthor Commented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
RoonaanCommented:
No, then it is not possible,

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

class myNewClass extends myBigClass {
}

-r-
0
 
m1tk4Commented:
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
 
headzooAuthor Commented:
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
 
m1tk4Commented:
>>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
 
RoonaanCommented:
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
 
headzooAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.