include() in a class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

include() in a class

Post by ed209 »

I'd like to be able to include a lot of arrays into a class I have put together. I use the arrays elsewhere on the site but the class also needs to use them.

Originally I had the arrays in 2 places (in the class and duplicated in another file for the rest of the site to use) but I would like to just have it all in one place but I can't seem to include() in a class.

Code: Select all

class myClass{
   include("pathToMy/inlude_file.php");
   
   //the rest of my class code goes in here

}
I get the error (this error was when I tried reqire_once() but also happens with include() and require()):

Code: Select all

Parse error: parse error, unexpected T_REQUIRE_ONCE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in
is it possible to include() in a class?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

It works when the array is returned though.
temp1.php

Code: Select all

<?php
return array ('x', 'y', 'z');
?>
temp2.php

Code: Select all

<?php
class cls1
 {
        var $arr;
        function cls1()
         {
                $this->arr = include_once("temp1.php");
         }
        function foo()
         {
                echo $this->arr[1];
         }
 }
$o = new cls1();
$o->foo();
?>
http://localhost/temp2.php outputs y
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

thanks for the reply, I didn't know that. The trouble is, the included file has more than one array in it, so

Code: Select all

$this->arr = include_once("temp1.php");
$this->arr would hold all of the arryas that were present in the included file?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

$this->arr would hold all of the arryas that were present in the included file?
$this->arr would hold whatever is returned by temp1.php since $this->arr is being assigned to include_once()

What you could do is $this->arr = include_once("temp1.php?ret=ar1");

This way you can check for existance of $_GET['ret'] (using isset($_GET['ret'])) and return the array ar1
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Keep things simple. Just do it like this:

Code: Select all

include("pathToMy/include_file.php");

class myClass{
      //the rest of my class code goes in here
}
That way all variables in file "include_file.php" will have global scope within the class, if that is what you intend. For more info reg. scope see http://www.php.net/variables.scope

The reason your example produced an error was because you included it in the wrong place. You can't include between class definition, methods and/or constructor. You can declare class properties there, but that's it.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

patrikG wrote: The reason your example produced an error was because you included it in the wrong place. You can't include between class definition, methods and/or constructor. You can declare class properties there, but that's it.
But in temp2.php (my first post) I had the line

Code: Select all

$this->arr = include_once("temp1.php");
in the constructor and it worked.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

include_once() only returns true or false, it doesn't return the output of the file.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

anjanesh wrote:
patrikG wrote: The reason your example produced an error was because you included it in the wrong place. You can't include between class definition, methods and/or constructor. You can declare class properties there, but that's it.
But in temp2.php (my first post) I had the line

Code: Select all

$this->arr = include_once("temp1.php");
in the constructor and it worked.
Of course it worked. You set a class property - and that's all you can do there.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

if you really need to place the include inside the class definition, then you must place it inside a method (class function)

Code: Select all

class myClass
{
   
  function myClass ()
  {
    include("pathToMy/inlude_file.php");
  }
    //the rest of my class code goes in here

}
But as has been suggested, it is better to use the include outside of the deifnition so it has the global scope.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Jenk wrote:if you really need to place the include inside the class definition, then you must place it inside a method (class function)

Code: Select all

class myClass
{
   
  function myClass ()
  {
    include("pathToMy/inlude_file.php");
  }
    //the rest of my class code goes in here

}
Sorry to be nitpicking (don't ask me, it's apparently one of those days), the class-method you've defined there is called the constructor as it gets executed every time you instantiate the class. A method only gets executed when explicitely called.

E.g.

Code: Select all

class myClass
{
   
  function myClass ()
// the constructor of the class
  {
    include("pathToMy/inlude_file.php");
  }

   function IAmAMethod($some_arguments){
   ...
  }
} //the rest of my class code goes in here

}
Post Reply