Connecting Tech Pros Worldwide Forums | Help | Site Map

php classes within classes?

thelobster@gmail.com
Guest
 
Posts: n/a
#1: Jul 17 '05
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,
thelobster@gmail.com


Jan Pieter Kunst
Guest
 
Posts: n/a
#2: Jul 17 '05

re: php classes within classes?


thelobster@gmail.com wrote:
[color=blue]
> 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.[/color]

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, <devnull@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Janwillem Borleffs
Guest
 
Posts: n/a
#3: Jul 17 '05

re: php classes within classes?


thelobster@gmail.com wrote:[color=blue]
> index.php will disply a blank page instead of 'FooBar Works!'.
>[/color]

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



TheLobster at Gmail dot Com
Guest
 
Posts: n/a
#4: Jul 17 '05

re: php classes within classes?


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.

Tony Marston
Guest
 
Posts: n/a
#5: Jul 17 '05

re: php classes within classes?



"TheLobster at Gmail dot Com" <thelobster@gmail.com> wrote in message
news:1104563309.628249.205680@z14g2000cwz.googlegr oups.com...[color=blue]
> 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"[/color]

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.
[color=blue]
> and globals cause problems if you want to make the
> code flexable.[/color]

Globals do not cause problems. Bad programming causes problems.

--
Tony Marston

http://www.tonymarston.net



Chung Leong
Guest
 
Posts: n/a
#6: Jul 17 '05

re: php classes within classes?


"Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
news:cr5tdn$gsv$1$8300dec7@news.demon.co.uk...[color=blue]
>
> "TheLobster at Gmail dot Com" <thelobster@gmail.com> wrote in message
> news:1104563309.628249.205680@z14g2000cwz.googlegr oups.com...[color=green]
> > 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"[/color]
>
> Utter crap! Globals are provided in many languages and serve their[/color]
purpose.[color=blue]
> They are neither 'good' or 'bad' in themsleves. It just depends on how you
> use them.
>[color=green]
> > and globals cause problems if you want to make the
> > code flexable.[/color]
>
> Globals do not cause problems. Bad programming causes problems.[/color]

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


Closed Thread