473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3068
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Cat | last post by:
Is there anything wrong with using a lot of include files? On one part of my website, I have a form. The page it posts data to, includes a different file based on some of the values. The...
1
by: Holden | last post by:
Hello, I am using Visual C++ 6.0 with Service Pack 5 installed and I have run into a problem that I haven't been able to figure out. I have written two classes and I want them to have pointers to...
10
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having...
28
by: Steven T. Hatton | last post by:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a situation where you had to chose between using...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
6
by: Ben Taylor | last post by:
I've asked about includes before in this group in order to stop files being #included either twice or not at all, and somebody said to use extern keyword for global variables that need to be used...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
5
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h"...
2
by: joe t. | last post by:
My apologies, i'm not sure how to ask this question correctly, so i'll give the examples as i go. First, i *think* my problem is described here, but i may be misunderstanding:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.