Can a class instance a property of another class
11 answers - 853 bytes -

Hey - -- -
Here I am again. Anybody still working on a Friday?
I would like to have a class instance be the property of another
class, like can be done in other languages. For example: I would like
to have a "Connections" class which contains all of the database
connection logic and query results. There are advantages to having
this type of utility class be local to a data or business class. (I
know that I could have a generic "include" with functions outside of
the class hierarchy.)
So, in the __construct method of a business or data class, for
example, one could:
include_once("connection_classes.kbk");
$this->connection_class = new connection_class;
This syntax fails, so I know this isn't right, but I hope you get the
idea.
Can it be done?
TIA, again
Ken
No.1 | | 1627 bytes |
| 
Ken Kixmoeller -- reply to ken (AT) kixmoeller (DOT) com wrote:
Hey - -- -
Here I am again. Anybody still working on a Friday?
I would like to have a class instance be the property of another class,
like can be done in other languages. For example: I would like to have a
"Connections" class which contains all of the database connection logic
and query results. There are advantages to having this type of utility
class be local to a data or business class. (I know that I could have a
generic "include" with functions outside of the class hierarchy.)
So, in the __construct method of a business or data class, for example,
one could:
include_once("connection_classes.kbk");
$this->connection_class = new connection_class;
This syntax fails, so I know this isn't right, but I hope you get the idea.
what fails? I can't smell the error your getting from here and from what I see
there should be no problem:
class Foo
{
private $var;
function __construct() { $this->var = "foo"; }
function getFoo() { return $this->var; }
}
class Bar
{
private $var;
private $foo;
function __construct() { $this->var = "bar"; $this->foo = new Foo; }
function getBar() { return $this->var; }
function speak() { echo "I am ",$this->foo->getFoo(),$this->getBar(),"\n"; }
}
$b = new Bar;
$b->speak();
Can it be done?
yes. see above. :-)
TIA, again
Ken
General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
No.2 | | 500 bytes |
| 
# Kixjaguar (AT) comcast (DOT) net / 2007-01-26 17:18:37 -0600:
So, in the __construct method of a business or data class, for
example, one could:
include_once("connection_classes.kbk");
$this->connection_class = new connection_class;
This syntax fails, so I know this isn't right, but I hope you get the
idea.
And we have to guess the error message? I don't see anything wrong
with the syntax. Post a small but complete script, and the error message.
No.3 | | 1394 bytes |
| 
Hey - -- -
Here I am again. Anybody still working on a Friday?
I would like to have a class instance be the property of another
class, like can be done in other languages. For example: I would like
to have a "Connections" class which contains all of the database
connection logic and query results. There are advantages to having
this type of utility class be local to a data or business class. (I
know that I could have a generic "include" with functions outside of
the class hierarchy.)
So, in the __construct method of a business or data class, for
example, one could:
include_once("connection_classes.kbk");
$this->connection_class = new connection_class;
This syntax fails, so I know this isn't right, but I hope you get the
idea.
This fails simply because you've omitted the parentheses on the
constructor. It should be:
$this->connection_class = new connection_class();
course, you may want/need to pass some arguments here too.
As mentioned by others though, for the scenario you describe above, you
would probably be better extending a base class.
Edward
Can it be done?
TIA, again
Ken
General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>
No.4 | | 1892 bytes |
| 
Edward Kay wrote:
>Hey - -- -
>>
>Here I am again. Anybody still working on a Friday?
>>
>I would like to have a class instance be the property of another
>class, like can be done in other languages. For example: I would like
>to have a "Connections" class which contains all of the database
>connection logic and query results. There are advantages to having
>this type of utility class be local to a data or business class. (I
>know that I could have a generic "include" with functions outside of
>the class hierarchy.)
>>
>So, in the __construct method of a business or data class, for
>example, one could:
>>
>include_once("connection_classes.kbk");
>$this->connection_class = new connection_class;
>>
>This syntax fails, so I know this isn't right, but I hope you get the
>idea.
This fails simply because you've omitted the parentheses on the
constructor. It should be:
$this->connection_class = new connection_class();
WRNG - the parentheses are optional - my example in a a previous reply
to this thread proves this (if you care to test it).
course, you may want/need to pass some arguments here too.
As mentioned by others though, for the scenario you describe above, you
would probably be better extending a base class.
I see no real justification for class extension at this stage, it *may* be the best
way to tackle this situation BUT there may be good reasons that the class in question
lives in it's own seperate class heirarchy.
object aggregation is just as valid as class extending in principle.
No.5 | | 239 bytes |
| 
Thanks for your help, guys. I had to leave my office last evening
before I had a chance to try any of them.
I am sneaking in some office time today. I'll let you know (with
complete scripts and error messages).
Ken
No.6 | | 907 bytes |
| 
K, Jochem, I adapted your example and got it working. Thank you very
much.
I am still playing with it to better understand. thing I don't
yet understand is the necessity for the getFoo()/getBar()
"handshake," especially the getbar() in the BAR class. That doesn't
seem to serve any purpose. My adaptation us just a getDummy().
Do they just serve to pass the object by reference?
Ken
Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:
class Foo
{
private $var;
function __construct() { $this->var = "foo"; }
function getFoo() { return $this->var; }
}
class Bar
{
private $var;
private $foo;
function __construct() { $this->var = "bar"; $this->foo = new Foo; }
function getBar() { return $this->var; }
function speak() { echo "I am ",$this->foo->getFoo(),$this->getBar
(),"\n"; }
}
--
No.7 | | 1788 bytes |
| 
Ken Kixmoeller -- reply to ken (AT) kixmoeller (DOT) com wrote:
K, Jochem, I adapted your example and got it working. Thank you very
much.
I am still playing with it to better understand. thing I don't yet
understand is the necessity for the getFoo()/getBar() "handshake,"
especially the getbar() in the BAR class. That doesn't seem to serve
any purpose. My adaptation us just a getDummy().
Do they just serve to pass the object by reference?
--
Ken
Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:
>
>
>>
>class Foo
>{
>private $var;
>function __construct() { $this->var = "foo"; }
>function getFoo() { return $this->var; }
>}
>>
>class Bar
>{
>private $var;
>private $foo;
>function __construct() { $this->var = "bar"; $this->foo = new Foo; }
>function getBar() { return $this->var; }
>function speak() { echo "I am
>",$this->foo->getFoo(),$this->getBar(),"\n"; }
>}
>>
>>
>
General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Assigning another class to a class variable is a standard technique,
so it's not surprising that it's implemented in PHP. I believe it's
called "composition", though that term seems to have a wider definition.
What is it that seems odd about what you call the getFoo()/getBar()
"handshake" ? Actually, what do you mean by that? I could be missing
something, but at the moment the two classes and their calls seem pretty
straight-forward.
No.8 | | 2665 bytes |
| 
Ken Kixmoeller -- reply to ken (AT) kixmoeller (DOT) com wrote:
K, Jochem, I adapted your example and got it working. Thank you very much.
I am still playing with it to better understand. thing I don't yet
understand is the necessity for the getFoo()/getBar() "handshake,"
especially the getbar() in the BAR class. That doesn't seem to serve any
purpose. My adaptation us just a getDummy().
Do they just serve to pass the object by reference?
no - the whole piece of code was just a silly example to show you
that you can stuff an object into the property of another object.
objects are always by reference in php5 - in php4 you have to
use the 'reference' operator (the @ symbol) to make object be passed by reference.
your original question showed a new object being made in the constructor
of another object - that is fine but it seems a little pointless to
worry about referencing some 'global' [connection] object in each relevant
class when you seem to be creating a new object in each constructor.
try this example (I assume you use php5 - which I think you are because
you mentioned using the __construct() method which is a php5 only bit of
functionality):
<?php
class DBConn {
private $userCnt = 0;
function incrementUC() { $this->userCnt++; }
function getUC() { return $this->userCnt; }
}
class DExample {
private $dbconn;
function __construct() {
$this->dbconn = getDBConn();
$this->dbconn->incrementUC();
}
}
function getDBConn() {
static $conn;
if (!isset($conn))
$conn = new DBConn();
return $conn;
}
$a = new DExample;
$c = new DExample;
$b = new DExample;
$d = getDBConn();
echo $d->getUC(),"\n";
?>
Ken
Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:
>>
>class Foo
>{
>private $var;
>function __construct() { $this->var = "foo"; }
>function getFoo() { return $this->var; }
>}
>>
>class Bar
>{
>private $var;
>private $foo;
>function __construct() { $this->var = "bar"; $this->foo = new Foo; }
>function getBar() { return $this->var; }
>function speak() { echo "I am
>",$this->foo->getFoo(),$this->getBar(),"\n"; }
>}
>>
>>
General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
No.9 | | 372 bytes |
| 
Sat, January 27, 2007 7:00 pm, Jochem Maas wrote:
objects are always by reference in php5 - in php4 you have to
use the 'reference' operator (the @ symbol) to make object be passed
by reference.
[pedantic for="archives"]
Not the @ symbol, which suppresses errors, but the & symbol which
forces a reference instead of a copy.
[/pedantic]
No.10 | | 529 bytes |
| 
Richard Lynch wrote:
Sat, January 27, 2007 7:00 pm, Jochem Maas wrote:
>objects are always by reference in php5 - in php4 you have to
>use the 'reference' operator (the @ symbol) to make object be passed
>by reference.
[pedantic for="archives"]
Not the @ symbol, which suppresses errors, but the & symbol which
forces a reference instead of a copy.
[/pedantic]
ah yes - whoops - my bad, twas probably late and I wasn't paying attention anymore.
No.11 | | 130 bytes |
| 
Thanks to all -- got all of this working fine. Mostly my syntax was a
bit off. Your examples helped me mend my ways.
Ken