Allen,
The short answer (but don't follow this):
<?php
class Meetgreet {
public function deleteSingle($id, $number) {
// do something
global $Notify;
$Notify->addToQ( .. );
}
}
?>
The long(er) answer:
I assume your Notifier object functions as singleton? Ie; accross your
entire application, there is only one instance of that class?
Why not go-static? That is, to my experience, the sweetest way to make
something globally accessible - without making something global. Like so
<?php
class Notifier {
protected static $queue = Array();
// make sure it can't be instantiated
private constructer __construct() {
}
public static function addToQ( $arg, $anotherArg) {
self::$queue[] = $arg.' - '.$anotherArg;
}
}
// and then from within any method anywhere, call
Notifier::addToQ('foo', 'bar');
?>
Does that work for you?
Regards,
Wouter
(ps. call me a purist, but a function defined in a class is no longer called
a function, but a *method*)
2009/12/15 Allen McCabe <[email protected]>
> Hey all (and Nirmalya, thanks for the help!),
>
>
> I have a question that I just can't seem to find via Google.
>
> I want to be able to add messages to a qeue whenever my classes complete
> (or
> fail to complete) specific functions. I think have a call within my html to
> my Notifier class to print all qeued messages (via a function 'printQ').
>
> How do I access a globally instantiated class from within another class?
>
> Example:
>
> <?php
>
> // INSTANTIATE
> $Meetgreet = new Meetgreet;
> $Notify = new Notifier;
>
> ...
> ...
>
> $Meetgreet->deleteSingle($id, 1); // This completes a function within
> Meetgreet class. That function needs to be able to use the Notifier
> function
> addtoQ(), how would this be accomplished?
>
> ?>
> ...
> ...
>
> <?php $Notify->printQ() ?>
>
> On Mon, Dec 14, 2009 at 6:07 PM, Nirmalya Lahiri
> <[email protected]>wrote:
>
> > --- On Tue, 12/15/09, Allen McCabe <[email protected]> wrote:
> >
> > > From: Allen McCabe <[email protected]>
> > > Subject: [PHP] Class not functioning
> > > To: "phpList" <[email protected]>
> > > Date: Tuesday, December 15, 2009, 6:17 AM
> > > Hey everyone, I just delved into
> > > classes recently and have been having
> > > moderate success so far.
> > >
> > > I have a puzzler though.
> > >
> > > I have the following class decalred and instantiated:
> > >
> > > class Notify {
> > > var $q = array();
> > >
> > > public function addtoQ($string, $class)
> > > {
> > > $message = '<span class="'. $class .'">'.
> > > $string .'</span>';
> > > $this->q[] = $message;
> > > }
> > >
> > > public function printQ()
> > > {
> > > if (isset($q))
> > > {
> > > echo '<p align="center"
> > > class="notification">';
> > > foreach($this->q as $msg)
> > > {
> > > echo $msg ."\n";
> > > }
> > > echo '</p>';
> > > }
> > >
> > > return;
> > > }
> > >
> > > function __destruct()
> > > {
> > > if (isset($q))
> > > {
> > > unset($this->q);
> > > }
> > > }
> > > } // END CLASS Notify
> > >
> > >
> > > And in my script, I call it like so:
> > > $Notif = new Notify;
> > >
> > > I have run other statements in other classes that should be
> > > adding to the $q
> > > array (ie. Notify::addtoQ('ERROR! There Was An Error
> > > Updating The
> > > Database!', 'error');)
> > >
> > > However, when I try to get my webpage to display them
> > > using:
> > >
> > > $Notify->printQ();
> > >
> > > it does not seem to want to loop through this array (and
> > > print the
> > > messages). I am getting NO error message, in fact
> > > everything 'looks' fine,
> > > I'm just not seeing the appropriate message.
> > >
> > > Any help would be appreicated!
> > >
> >
> > Allen,
> > You have made a small typing mistake in function printQ() where you
> would
> > like to checked the array for its existence. By mistake you have wrote
> "if
> > (isset($q))". But your array variable is not an freely accessible
> array,the
> > array is embedded into an object. So, you have to write the like "if
> > (isset($this->q))".
> >
> > Another point, you can't add a message into the array by calling the
> > member function addtoQ() using scope resolution operator "::". If you
> really
> > want to add message into the array, you have to call the member function
> > from within the object. (ie. $Notif->addtoQ('ERROR! There Was An Error
> > Updating The Database!', 'error');).
> >
> > ---
> > নির্মাল্য লাহিড়ী [Nirmalya Lahiri]
> > +৯১-৯৪৩৩১১৩৫৩৬ [+91-9433113536]
> >
> >
> >
> >
> >
>
--
http://www.interpotential.com
http://www.ilikealot.com
Phone: +4520371433