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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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-
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
RoonaanCommented:
No, then it is not possible,

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

class myNewClass extends myBigClass {
}

-r-
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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?






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.
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.
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-
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.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.