473,396 Members | 2,033 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,396 software developers and data experts.

php classes within classes?

Hey,
I've been programming PHP for about 2 years and have dabbled with
classes. I'm working on a project and can't seam to figure out how to
use classes within classes. For example:

--foo.class.php--
<?
class Foo
{
var $test1;
function Foo()
{
require_once('bar.class.php');
$test1=new Bar();
}

function testFooBar()
{
return $this->test1->testBar();
}
}
?>

--bar.class.php--
<?
class Bar
{
var $return;
function Bar()
{
$this->return='FooBar Works!';
}

function testBar()
{
return $this->return;
}
}
?>

--index.php--
<?
require_once('foo.class.php');
$test = new Foo();
echo $test->testFooBar();
?>

index.php will disply a blank page instead of 'FooBar Works!'.

The application of this is to use a MySQL wrapper that I made inside of
another class so that i can do something like $app->genUserList(); and
have it automadically run the SQL query and generate the list for me. I
could just take the functions from the SQL wrapper, but I'd rather not.
I want to have the flexability of adding in new features that use other
classes without the hassle of making the inital class super huge.
Thanks,
th********@gmail.com

Jul 17 '05 #1
5 1712
th********@gmail.com wrote:
The application of this is to use a MySQL wrapper that I made inside of
another class so that i can do something like $app->genUserList(); and
have it automadically run the SQL query and generate the list for me. I
could just take the functions from the SQL wrapper, but I'd rather not.
I want to have the flexability of adding in new features that use other
classes without the hassle of making the inital class super huge.


I don't think you can define a class within another class, but you can
instantiate a class in another class if they are defined in the right order.

Like this:

class Bar {
// class definition
}
class Foo {

var $test1;

function Foo() {
// Foo knows about Bar because it was defined earlier
$this->test1 =& new Bar();
}
}

(untested)

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #2
th********@gmail.com wrote:
index.php will disply a blank page instead of 'FooBar Works!'.


The body of the Foo constructor should be:

function Foo() {
require_once('bar.class.php');
$this->test1=new Bar();
}

Check your error logs or enable the display_errors directive in your php.ini
file.
JW

Jul 17 '05 #3
You are 100% correct, it should be $this->test1= new bar(); however it
doesn't work. I am NOT getting an error, although thinking about it, I
haven't tried a E_ALL reporting, just E_ERROR. I'll test that out in
the morning and if anything new arrises then I'll let ya all know. I
know Java and C++ well. Along the same note, I know that PHP doesn't
support namespace yet. The real goal is to get it so that I can access
a class from within another class. I don't want to use globals because
as I once so elequently saw it, "it's just a small step down from
globals to goto's" and globals cause problems if you want to make the
code flexable.

Jul 17 '05 #4

"TheLobster at Gmail dot Com" <th********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
You are 100% correct, it should be $this->test1= new bar(); however it
doesn't work. I am NOT getting an error, although thinking about it, I
haven't tried a E_ALL reporting, just E_ERROR. I'll test that out in
the morning and if anything new arrises then I'll let ya all know. I
know Java and C++ well. Along the same note, I know that PHP doesn't
support namespace yet. The real goal is to get it so that I can access
a class from within another class. I don't want to use globals because
as I once so elequently saw it, "it's just a small step down from
globals to goto's"
Utter crap! Globals are provided in many languages and serve their purpose.
They are neither 'good' or 'bad' in themsleves. It just depends on how you
use them.
and globals cause problems if you want to make the
code flexable.


Globals do not cause problems. Bad programming causes problems.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #5
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:cr*******************@news.demon.co.uk...

"TheLobster at Gmail dot Com" <th********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
You are 100% correct, it should be $this->test1= new bar(); however it
doesn't work. I am NOT getting an error, although thinking about it, I
haven't tried a E_ALL reporting, just E_ERROR. I'll test that out in
the morning and if anything new arrises then I'll let ya all know. I
know Java and C++ well. Along the same note, I know that PHP doesn't
support namespace yet. The real goal is to get it so that I can access
a class from within another class. I don't want to use globals because
as I once so elequently saw it, "it's just a small step down from
globals to goto's"
Utter crap! Globals are provided in many languages and serve their

purpose. They are neither 'good' or 'bad' in themsleves. It just depends on how you
use them.
and globals cause problems if you want to make the
code flexable.


Globals do not cause problems. Bad programming causes problems.


Wrap a class around a global, call it a "singleton," and suddenly you have a
something that's highly reusable and what not.
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming...
15
by: Michael | last post by:
I've written a simple class that puts together a MySQL SELECT query and allows you to extend the query, but I'm unsure as to when to use $this - > var_name and when I can just use $varname, and...
1
by: Miranda Evans | last post by:
Seeking reference material (a url, a book, an article) that offers advice and guidelines for organizing classes within files. For example, assume two classes: 1) SuperABC - a superclass 2)...
12
by: williamc | last post by:
Is there anything wrong with having several classes with the same name in the same style sheet? Something like... div.pagedown { margin: 20px 0px 20px 0px; border-top: 1px solid #caa;...
9
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace...
5
by: Paul Bromley | last post by:
I have written a similar enquiry to this newsgroup, but had no responses - hence I will rephrase it with the hope that someone will answer. I am new to using Classes, but trying hard to get the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
6
by: moondaddy | last post by:
I'm new to c# and am wondering if its possible to access members of a nested class. Can someone please advise? Thanks. class Program { static void Main(string args) { try { Test1 obj =...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Clive Dixon | last post by:
When working with lots of associated "supporting" classes alongside classes (by this, I mean things such as associated component editor classes specified by , debugger proxy classes specified by ...
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: 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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.