Re: Class not functioning (RESOLVED)

Re: Class not functioning (RESOLVED)

am 19.12.2009 06:07:52 von Allen McCabe

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

I looked into registries (singleton registries), and while I can see the
advantage they provide, most every article I read advised AGAINST using
singleton registries because it creates extra dependencies (ie. a file need=
s
the database class AND the registry class instead of just the database
class). The disadvantage to me about this is having to keep everything
straight; my projects as of late have been expanding rapidly and I can
barely keep track of everything. As it stands, it takes me about 30 minutes
to get back into my projects each day.

Wouter; I ended up going with the static option and it works nicely :) I am
thrilled at how it just works! Coupled with some nicely styled DIV layers
and some onclick=3Dthis.style.display=3D'none'; to make it go away I have a
fantastic user notification system.

On Wed, Dec 16, 2009 at 3:59 AM, Wouter van Vliet / Interpotential <
public@interpotential.com> wrote:

> Allen,
>
> Before you go with my static-approach, please do consider Shawn's registr=
y
> pattern suggestion. That's pretty sweet too ;-).
>
> A little response to your long text, before I help you fix the bug. A
> static 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 an object. They are just there.
>
> Now, about your bug. The syntax for referencing a static property is a bi=
t
> weird - which has to do with the existence of class constants, which migh=
t
> have set you off.
>
> Notifier::notifyQueue would reference a class constant. The [] syntax is
> not valid here, since a constant is - you got it: constant. And thus cann=
ot
> be changed.
> Notifier::$notifyQ[] =3D '

...
'; references the static proper=
ty.
>
> But... since notifyQ is a proptected static property, it is very unlikeal=
y
> 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 =
into
>> trouble assigning new values to the static array.
>>
>> I am getting a "syntax error, unexpected '[' " on this line of my Notifi=
er
>> 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 s=
o
>>>
>>> >>> 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 comple=
te
>>>> (or
>>>> fail to complete) specific functions. I think have a call within my ht=
ml
>>>> to
>>>> my Notifier class to print all qeued messages (via a function 'printQ'=
).
>>>>
>>>> How do I access a globally instantiated class from within another clas=
s?
>>>>
>>>> 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 wro=
te
>>>> "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 th=
e
>>>> > member function addtoQ() using scope resolution operator "::". If yo=
u
>>>> 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 Err=
or
>>>> > Updating The Database!', 'error');).
>>>> >
>>>> > ---
>>>> > নির্মাঠ²à=
§à¦=AF লাহিড়=E0=
§€ [Nirmalya Lahiri]
>>>> > +৯১-৯৪৩৩=E0= A7§à=
§§à§©à§«à§©à§¬ [+91-9433113536]
>>>> >
>>>> >
>>>> >
>>>> >
>>>> >
>>>>
>>>
>>>
>>>
>>> --
>>> http://www.interpotential.com
>>> http://www.ilikealot.com
>>>
>>> Phone: +4520371433
>>>
>>
>>
>
>
> --
> http://www.interpotential.com
> http://www.ilikealot.com
>
> Phone: +4520371433
>

--000e0cd28da4a6dfbf047b0dd667--