Class not functioning

Class not functioning

am 15.12.2009 01:47:19 von Allen McCabe

--00504502b0ea7d9875047ab9bbd0
Content-Type: text/plain; charset=ISO-8859-1

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 = ''. $string .'';
$this->q[] = $message;
}

public function printQ()
{
if (isset($q))
{
echo '

';
foreach($this->q as $msg)
{
echo $msg ."\n";
}
echo '

';
}

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!

--00504502b0ea7d9875047ab9bbd0--

Re: Class not functioning

am 15.12.2009 03:07:39 von Nirmalya Lahiri

--- On Tue, 12/15/09, Allen McCabe wrote: > Fr=
om: Allen McCabe =0A> Subject: [PHP] Class not funct=
ioning=0A> To: "phpList" =0A> Date: Tuesday, Dec=
ember 15, 2009, 6:17 AM=0A> Hey everyone, I just delved into=0A> classes re=
cently and have been having=0A> moderate success so far.=0A> =0A> I have a =
puzzler though.=0A> =0A> I have the following class decalred and instantiat=
ed:=0A> =0A> class Notify {=0A> var $q =3D array();=0A> =0A> public funct=
ion addtoQ($string, $class)=0A> {=0A>   $message =3D ' '. $class .'">'.=0A> $string .'';=0A>   $this->q[] =3D $message=
;=0A> }=0A> =0A> public function printQ()=0A> {=0A>   if (isset($q)=
)=0A>   {=0A>    echo '

class=3D=
"notification">';=0A>    foreach($this->q as $msg)=0A> =C2=
 Â Â=A0{=0A>     echo $msg ."\n";=0A>    }=
=0A>    echo '

';=0A>   }=0A> =0A>   return;=0A=
> }=0A> =0A> function __destruct()=0A> {=0A>   if (isset($q))=0A> =
  {=0A>    unset($this->q);=0A>   }=0A> }=0A> } /=
/ END CLASS Notify=0A> =0A> =0A> And in my script, I call it like so:=0A> $=
Notif =3D new Notify;=0A> =0A> I have run other statements in other classes=
that should be=0A> adding to the $q=0A> array (ie. Notify::addtoQ('ERROR! =
There Was An Error=0A> Updating The=0A> Database!', 'error');)=0A> =0A> How=
ever, when I try to get my webpage to display them=0A> using:=0A> =0A> $Not=
ify->printQ();=0A> =0A> it does not seem to want to loop through this array=
(and=0A> print the=0A> messages). I am getting NO error message, in fact=
=0A> everything 'looks' fine,=0A> I'm just not seeing the appropriate messa=
ge.=0A> =0A> Any help would be appreicated!=0A> Allen,=0A You have ma=
de a small typing mistake in function printQ() where you would like to chec=
ked 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 em=
bedded 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 t=
he member function addtoQ() using scope resolution operator "::". If you re=
ally want to add message into the array, you have to call the member functi=
on from within the object. (ie. $Notif->addtoQ('ERROR! There Was An Error U=
pdating The Database!', 'error');). --- নির=
্মাল্য লা=
হিড়ী [Nirmalya Lahiri]=0A+৯à=A7=
=A7-৯৪৩৩১১=E 0§©à§=
«à§©à§ [+91-9433113536]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Class not functioning

am 15.12.2009 22:14:22 von Allen McCabe

--00504502c06fbfd3b4047acadfc8
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

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 (o=
r
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:


// INSTANTIATE
$Meetgreet =3D new Meetgreet;
$Notify =3D new Notifier;

....
....

$Meetgreet->deleteSingle($id, 1); // This completes a function within
Meetgreet class. That function needs to be able to use the Notifier functio=
n
addtoQ(), how would this be accomplished?

?>
....
....

printQ() ?>

On Mon, Dec 14, 2009 at 6:07 PM, Nirmalya Lahiri
wrote:

> --- On Tue, 12/15/09, Allen McCabe wrote:
>
> > From: Allen McCabe
> > Subject: [PHP] Class not functioning
> > To: "phpList"
> > 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 =3D array();
> >
> > public function addtoQ($string, $class)
> > {
> > $message =3D ''.
> > $string .'
';
> > $this->q[] =3D $message;
> > }
> >
> > public function printQ()
> > {
> > if (isset($q))
> > {
> > echo '

> > class=3D"notification">';
> > foreach($this->q as $msg)
> > {
> > echo $msg ."\n";
> > }
> > echo '

';
> > }
> >
> > return;
> > }
> >
> > function __destruct()
> > {
> > if (isset($q))
> > {
> > unset($this->q);
> > }
> > }
> > } // END CLASS Notify
> >
> >
> > And in my script, I call it like so:
> > $Notif =3D 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 woul=
d
> like to checked the array for its existence. By mistake you have wrote "i=
f
> (isset($q))". But your array variable is not an freely accessible array,t=
he
> 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 rea=
lly
> 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');).
>
> ---
> নির্মাঠ²à§=
à¦=AF লাহিড়ী [Nirmalya =
Lahiri]
> +৯১-৯৪৩৩=E0= A7§à=A7=
§à§©à§«à§©à§ [+91-9433113536]
>
>
>
>
>

--00504502c06fbfd3b4047acadfc8--

Re: Class not functioning

am 15.12.2009 23:30:41 von Shawn McKenzie

Allen McCabe wrote:
> 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:
>
> >
> // 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?
>
> ?>
> ...
> ...
>

Several ways to do it. One would be to make the Meetgreet a singleton
if it really is, and then use its static getInstance. I would probably
use a registry pattern though.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Class not functioning

am 15.12.2009 23:30:49 von public

--000e0cd1b36e247fd0047acbf166
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Allen,

The short answer (but don't follow this):
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

class Notifier {

protected static $queue =3D Array();

// make sure it can't be instantiated
private constructer __construct() {
}

public static function addToQ( $arg, $anotherArg) {
self::$queue[] =3D $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 calle=
d
a function, but a *method*)

2009/12/15 Allen McCabe

> 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:
>
> >
> // INSTANTIATE
> $Meetgreet =3D new Meetgreet;
> $Notify =3D 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?
>
> ?>
> ...
> ...
>
> printQ() ?>
>
> On Mon, Dec 14, 2009 at 6:07 PM, Nirmalya Lahiri
> wrote:
>
> > --- On Tue, 12/15/09, Allen McCabe wrote:
> >
> > > From: Allen McCabe
> > > Subject: [PHP] Class not functioning
> > > To: "phpList"
> > > 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 =3D array();
> > >
> > > public function addtoQ($string, $class)
> > > {
> > > $message =3D ''.
> > > $string .'
';
> > > $this->q[] =3D $message;
> > > }
> > >
> > > public function printQ()
> > > {
> > > if (isset($q))
> > > {
> > > echo '

> > > class=3D"notification">';
> > > foreach($this->q as $msg)
> > > {
> > > echo $msg ."\n";
> > > }
> > > echo '

';
> > > }
> > >
> > > return;
> > > }
> > >
> > > function __destruct()
> > > {
> > > if (isset($q))
> > > {
> > > unset($this->q);
> > > }
> > > }
> > > } // END CLASS Notify
> > >
> > >
> > > And in my script, I call it like so:
> > > $Notif =3D 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 functio=
n
> > from within the object. (ie. $Notif->addtoQ('ERROR! There Was An Error
> > Updating The Database!', 'error');).
> >
> > ---
> > নির্মাঠ²à=A7=
à¦¯ লাহিড়ৠ=
=80 [Nirmalya Lahiri]
> > +৯১-৯৪৩৩=E0= A7§à=A7=
§à§©à§«à§©à§ [+91-9433113536]
> >
> >
> >
> >
> >
>



--=20
http://www.interpotential.com
http://www.ilikealot.com

Phone: +4520371433

--000e0cd1b36e247fd0047acbf166--

Re: Class not functioning

am 16.12.2009 01:23:53 von Allen McCabe

--00504502c10d7c759b047acd8557
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Wouter,

Implementing your static idea was pretty easy, I was already referencing
Notifier with the :: operator in my other methods, however I am running int=
o
trouble assigning new values to the static array.

I am getting a "syntax error, unexpected '[' " on this line of my Notifier
class:

Notifier::notifyQ[] =3D '


.. . .

Any ideas why this is causing an error?
(note: I did try using $this->Notifier, and it said I cannot do what-not to
a non-object, can't remember the exact message at the moment)

On Tue, Dec 15, 2009 at 2:30 PM, Wouter van Vliet / Interpotential <
public@interpotential.com> wrote:

> Allen,
>
> The short answer (but don't follow this):
> > 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
>
> > class Notifier {
>
> protected static $queue =3D Array();
>
> // make sure it can't be instantiated
> private constructer __construct() {
> }
>
> public static function addToQ( $arg, $anotherArg) {
> self::$queue[] =3D $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
>
>> 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:
>>
>> >>
>> // INSTANTIATE
>> $Meetgreet =3D new Meetgreet;
>> $Notify =3D 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?
>>
>> ?>
>> ...
>> ...
>>
>> printQ() ?>
>>
>> On Mon, Dec 14, 2009 at 6:07 PM, Nirmalya Lahiri
>> wrote:
>>
>> > --- On Tue, 12/15/09, Allen McCabe wrote:
>> >
>> > > From: Allen McCabe
>> > > Subject: [PHP] Class not functioning
>> > > To: "phpList"
>> > > 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 =3D array();
>> > >
>> > > public function addtoQ($string, $class)
>> > > {
>> > > $message =3D ''.
>> > > $string .'
';
>> > > $this->q[] =3D $message;
>> > > }
>> > >
>> > > public function printQ()
>> > > {
>> > > if (isset($q))
>> > > {
>> > > echo '

>> > > class=3D"notification">';
>> > > foreach($this->q as $msg)
>> > > {
>> > > echo $msg ."\n";
>> > > }
>> > > echo '

';
>> > > }
>> > >
>> > > return;
>> > > }
>> > >
>> > > function __destruct()
>> > > {
>> > > if (isset($q))
>> > > {
>> > > unset($this->q);
>> > > }
>> > > }
>> > > } // END CLASS Notify
>> > >
>> > >
>> > > And in my script, I call it like so:
>> > > $Notif =3D 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 functi=
on
>> > from within the object. (ie. $Notif->addtoQ('ERROR! There Was An Error
>> > Updating The Database!', 'error');).
>> >
>> > ---
>> > নির্মাঠ²à=A7=
à¦¯ লাহিড়ৠ=
=80 [Nirmalya Lahiri]
>> > +৯১-৯৪৩৩=E0= A7§à=
§§à§©à§«à§©à§¬ [+91-9433113536]
>> >
>> >
>> >
>> >
>> >
>>
>
>
>
> --
> http://www.interpotential.com
> http://www.ilikealot.com
>
> Phone: +4520371433
>

--00504502c10d7c759b047acd8557--

Re: Class not functioning

am 16.12.2009 01:48:09 von Shawn McKenzie

Sorry, and then I didn't keep it on list :-(

Shawn McKenzie wrote:
> Please reply to the list. Just google for "php registry pattern". Here is a very basic example. There are better OOP people here than I.
>
> class Registry {
> protected $_objects = array();
>
> function set($name, &$object) {
> $this->_objects[$name] =& $object;
> }
>
> function &get($name) {
> return $this->_objects[$name];
> }
>
> }
>
> // in main code somewhere
> $Registry = new Registry;
> $Meetgreet = new Meetgreet;
> $Notify = new Notifier;
> $Registry->set('Meetgreet';, $Meetgreet);
> $Registry->set('Notify', $Notify);
>
> // in Meetgreet
> $Registry = new Registry;
> $Notify = $Registry->get('Notify');
>
> I'll post a singleton when I have more time.
>
> Allen McCabe wrote:
>> Thank you for your response! Your answer is intriguing, and I would
>> love some extrapolation!
>>
>> Meetgreet is only needed on 1, maybe 2 pages, so including it globally
>> is not needed, and because all meetgreet business is done on a single
>> page, so it is currently uncessary (however I will admit it isn't
>> extendible this way).
>>
>> So what is this registry pattern you speak of?
>>
>> Thanks!
>> Allen
>>
>> On Tue, Dec 15, 2009 at 2:30 PM, Shawn McKenzie >> > wrote:
>>
>> Allen McCabe wrote:
>> > 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:
>> >
>> > >> >
>> > // 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?
>> >
>> > ?>
>> > ...
>> > ...
>> >
>>
>> Several ways to do it. One would be to make the Meetgreet a singleton
>> if it really is, and then use its static getInstance. I would
>> probably
>> use a registry pattern though.
>>
>> --
>> Thanks!
>> -Shawn
>> http://www.spidean.com
>>
>>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Class not functioning

am 16.12.2009 12:59:15 von public

--000e0cd1b0c8543c05047ad73c77
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Allen,

Before you go with my static-approach, please do consider Shawn's registry
pattern suggestion. That's pretty sweet too ;-).

A little response to your long text, before I help you fix the bug. A stati=
c
property is basically the same as a regular property on an object. Only
difference is that they are not reset when the class is instantiated into a=
n
object. They are just there.

Now, about your bug. The syntax for referencing a static property is a bit
weird - which has to do with the existence of class constants, which might
have set you off.

Notifier::notifyQueue would reference a class constant. The [] syntax is no=
t
valid here, since a constant is - you got it: constant. And thus cannot be
changed.
Notifier::$notifyQ[] =3D '

...
'; references the static property=
..

But... since notifyQ is a proptected static property, it is very unlikealy
that you'll ever actually write Notifier::$notifyQ. You add to this queue
from within the class itself, so therefore self::$notifyQ is a lot better.

Does that answer your question?

Btw; Shawn; Assuming that your Registry class holds objects, there is no
need have the ampersand in front of the get method or $object argument.
Objects are *always* references. And you might want to look at the __get,
__set and __isset magic.

Wouter


2009/12/16 Allen McCabe

> Wouter,
>
> Implementing your static idea was pretty easy, I was already referencing
> Notifier with the :: operator in my other methods, however I am running i=
nto
> trouble assigning new values to the static array.
>
> I am getting a "syntax error, unexpected '[' " on this line of my Notifie=
r
> class:
>
> Notifier::notifyQ[] =3D '
>
> . . .
>
> Any ideas why this is causing an error?
> (note: I did try using $this->Notifier, and it said I cannot do what-not =
to
> a non-object, can't remember the exact message at the moment)
>
> On Tue, Dec 15, 2009 at 2:30 PM, Wouter van Vliet / Interpotential <
> public@interpotential.com> wrote:
>
>> Allen,
>>
>> The short answer (but don't follow this):
>> >> 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
>>
>> >> class Notifier {
>>
>> protected static $queue =3D Array();
>>
>> // make sure it can't be instantiated
>> private constructer __construct() {
>> }
>>
>> public static function addToQ( $arg, $anotherArg) {
>> self::$queue[] =3D $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
>>
>>> 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 complet=
e
>>> (or
>>> fail to complete) specific functions. I think have a call within my htm=
l
>>> 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:
>>>
>>> >>>
>>> // INSTANTIATE
>>> $Meetgreet =3D new Meetgreet;
>>> $Notify =3D 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?
>>>
>>> ?>
>>> ...
>>> ...
>>>
>>> printQ() ?>
>>>
>>> On Mon, Dec 14, 2009 at 6:07 PM, Nirmalya Lahiri
>>> wrote:
>>>
>>> > --- On Tue, 12/15/09, Allen McCabe wrote:
>>> >
>>> > > From: Allen McCabe
>>> > > Subject: [PHP] Class not functioning
>>> > > To: "phpList"
>>> > > 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 =3D array();
>>> > >
>>> > > public function addtoQ($string, $class)
>>> > > {
>>> > > $message =3D ''.
>>> > > $string .'
';
>>> > > $this->q[] =3D $message;
>>> > > }
>>> > >
>>> > > public function printQ()
>>> > > {
>>> > > if (isset($q))
>>> > > {
>>> > > echo '

>>> > > class=3D"notification">';
>>> > > foreach($this->q as $msg)
>>> > > {
>>> > > echo $msg ."\n";
>>> > > }
>>> > > echo '

';
>>> > > }
>>> > >
>>> > > return;
>>> > > }
>>> > >
>>> > > function __destruct()
>>> > > {
>>> > > if (isset($q))
>>> > > {
>>> > > unset($this->q);
>>> > > }
>>> > > }
>>> > > } // END CLASS Notify
>>> > >
>>> > >
>>> > > And in my script, I call it like so:
>>> > > $Notif =3D 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 wrot=
e
>>> "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 Erro=
r
>>> > Updating The Database!', 'error');).
>>> >
>>> > ---
>>> > নির্মাঠ²à=A7=
à¦¯ লাহিড়ৠ=
=80 [Nirmalya Lahiri]
>>> > +৯১-৯৪৩৩=E0= A7§à=
§§à§©à§«à§©à§¬ [+91-9433113536]
>>> >
>>> >
>>> >
>>> >
>>> >
>>>
>>
>>
>>
>> --
>> http://www.interpotential.com
>> http://www.ilikealot.com
>>
>> Phone: +4520371433
>>
>
>


--=20
http://www.interpotential.com
http://www.ilikealot.com

Phone: +4520371433

--000e0cd1b0c8543c05047ad73c77--