Wade,
I am trying to understand better why you don't like your first option
(passing in the object) as this is the object-oriented way of doing things.
It seems like the optimal solution as all you are doing is reading a
variable. I am guessing due to the referencing that you are trying to do
more than this. In these situations I often times use my factory or a
controller specific to Apple to house a list of all of the Apples. As the
factory or controller is a singleton object, it is easy to reference that
object and get the list of Apples (see example below).
A separate thought is making Orange a subclass of Apple. I am guessing this
won't work, but again, it fits the description provided so far.
class Orange {
public function dog($id) {
$the_farm = new Apple_Farm(); // will return same object every
time
echo $the_farm->get_apple($id)->cat;
}
}
class Apple_Farm {
// singleton stuff - http://php.net/manual/en/language.oop5.patterns.php
private $list_of_apples = array();
public function get_apple($id) {
foreach ($list_of_apples as $apple) {
if ($apple->id == $id) {
return $apple;
}
}
}
}
Justin Scott Giboney
On Mon, Oct 10, 2011 at 6:26 AM, <[email protected]> wrote:
> The value does need to be able to be changed.
>
>
>
> On Oct 9, 2011, at 23:37, Kevin Jensen <[email protected]> wrote:
>
> > It depends on the usage of your object's properties. If $cat is not going
> to
> > be changing you can just make it a constant of the Apple class:
> >
> > <?php
> >
> > class Apple {
> > const cat = "I'm a cat"; // HAS to be defined since it is a const
> >
> > public function __construct () {
> > $orange = new Orange();
> > $orange->dog();
> > }
> > }
> >
> > $apple = new Apple();
> >
> > class Orange {
> > public function dog () {
> > echo Apple::cat;
> > }
> > }
> >
> > ?>
> >
> > The downside to this is where it is a constant you cannot change the
> value
> > dynamically.
> >
> > On Sat, Oct 8, 2011 at 1:52 PM, Steve Meyers <[email protected]
> >wrote:
> >
> >> On 10/8/11 9:03 AM, Wade Preston Shearer wrote:
> >>> That gets me the simplicity I want insider of dog(), but adds
> >>> complexity ouside of dog each time I have to call it and complicates
> >>> things with other variables I have to pass in. Any way to pass it in
> >>> automatically? I'm thinking of __set and overloading concepts but
> >>> haven't been able to come up with a solution.
> >>
> >> You can use $GLOBALS or global, but that's certainly not ideal. Not
> >> that I've never done it before....
> >>
> >> Steve
> >>
> >> _______________________________________________
> >>
> >> UPHPU mailing list
> >> [email protected]
> >> http://uphpu.org/mailman/listinfo/uphpu
> >> IRC: #uphpu on irc.freenode.net
> >>
> >
> > _______________________________________________
> >
> > UPHPU mailing list
> > [email protected]
> > http://uphpu.org/mailman/listinfo/uphpu
> > IRC: #uphpu on irc.freenode.net
>
> _______________________________________________
>
> UPHPU mailing list
> [email protected]
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>
_______________________________________________
UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net