470,815 Members | 1,104 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

include file in class

hello!

Iam wondering how can I include file in class? is it possible?
is it possible to create object inside another object? for instance if I
create mysqli connection object inside another class is that doible? or
one should pass created objekt as parameter to another objekt?

regarding include it works if I do like this
PHP Code:
class myClass{
function(){
include 'func.php';
func(); //returns $var
//do something with $var
return $array;
}

function1(){
include 'func.php';
func();//returns $var
//do something with $var
return $array2;
}
}

but it doesn't make eny sence to include it over and over again in every
function so I would like to that get some thing like
PHP Code:
class myClass{
include 'func.php'; //but this don't work
func(); // $var=3

function(){
//do something with $var
return $array;
}

function(){
//do something with $var
return $array2;
}

any ideas or comments?

thanx in advance

Jan 3 '06 #1
4 2893
Carramba wrote:

Iam wondering how can I include file in class? is it possible?
is it possible to create object inside another object? for instance if I
create mysqli connection object inside another class is that doible? or
one should pass created objekt as parameter to another objekt?


Yes, but if you'd bothered to try it out you'd have found that although you
can include within a class/function/condition/loop the included code is
included in the global scope.

I suspect it may be possible to achieve the result you want but it's not
overly clear from your post what that is.

C.
Jan 4 '06 #2
Carramba wrote:
but it doesn't make eny sence to include it over and over again in
every function so I would like to that get some thing like
PHP Code:
class myClass{
include 'func.php'; //but this don't work
func(); // $var=3


That's where you can use the constructor for:

class myClass {
var $var;

function myClass() {
include 'include.php';
$this->var = getVar();
}
}

However, keep in mind that this is not a very good OO design. If you have a
collection of utility functions, the best way to use them is through
static/instantiated method calls or through inheritance:

Example A (static calls):

include 'Utils.php';

class myClass {
var $var;

function myClass() {
$this->var = Utils::getVar();
}
}
Example B (inheritance):

include 'Utils.php';

class myClass extends Utils {
var $var;

function myClass() {
$this->var = $this->getVar();
}
}
JW


Jan 4 '06 #3
thanx!

It seem to pass an object from a function one should declare

include 'somefile.php'; //have getObj() with returns object

class myClass {
var $var;

function myClass() {
$var = getObj();
}
}

then I was trying to use $this->var I ll only get error about unknown
variable var

Thanx again
Jan 4 '06 #4
Carramba wrote:
include 'somefile.php'; //have getObj() with returns object

class myClass {
var $var;

function myClass() {
$var = getObj();
}
}

then I was trying to use $this->var I ll only get error about unknown
variable var


Using: $this->var would be correct. When this triggers errors, the getObj()
function should be examined.
JW
Jan 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom Cat | last post: by
1 post views Thread by Holden | last post: by
10 posts views Thread by Toke H?iland-J?rgensen | last post: by
28 posts views Thread by Steven T. Hatton | last post: by
18 posts views Thread by Exits Funnel | last post: by
60 posts views Thread by Derrick Coetzee | last post: by
6 posts views Thread by Ben Taylor | last post: by
14 posts views Thread by Jon Rea | last post: by
5 posts views Thread by Tio | last post: by
2 posts views Thread by joe t. | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.