Page 1 of 1

include() in a class

Posted: Fri Dec 09, 2005 4:44 am
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?

Posted: Fri Dec 09, 2005 5:35 am
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

Posted: Fri Dec 09, 2005 5:46 am
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?

Posted: Fri Dec 09, 2005 5:53 am
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

Posted: Fri Dec 09, 2005 6:57 am
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.

Posted: Fri Dec 09, 2005 7:14 am
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.

Posted: Fri Dec 09, 2005 7:17 am
by Jenk
include_once() only returns true or false, it doesn't return the output of the file.

Posted: Fri Dec 09, 2005 7:22 am
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.

Posted: Fri Dec 09, 2005 7:36 am
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.

Posted: Fri Dec 09, 2005 7:45 am
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

}