By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

php classes within classes?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.