Re: Re: "use strict" or similar in PHP?
| From: | Robert Cummings | Date: | Sun, 01 Mar 2009 14:59:38 +0000 |
| Subject: | Re: Re: "use strict" or similar in PHP? | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-289016@lists.php.net to get a copy of this message | ||
On Sun, 2009-03-01 at 09:57 -0500, Robert Cummings wrote:
> On Sun, 2009-03-01 at 09:48 -0500, tedd wrote:
> > At 9:21 AM -0500 3/1/09, Robert Cummings wrote:
> > >On Sun, 2009-03-01 at 08:36 -0500, tedd wrote:
> > >> The OP asked:
> > >>
> > >> >Is there in PHP something like "use strict" from perl? I find it
> > >> >pretty
> > >> >annoying to need to run script over and over again just to find out that I
> > >> >made typo in variable name.
> > >>
> > >> And I've been waiting for an answer myself, but I haven't seen one.
> > >>
> > >> From what I remember, in perl if you use "use strict;" it requires to
> > >> to define your variables (my) before using them. If you make a
> > >> variable typo in your code, then you'll trigger an error when you try
> > >> to run it.
> > >>
> > >> From what I've seen of php, even with using strict error reporting,
> > >> you can do that all day long without generating an error.
> > >>
> > >> So the answer appears to be "No, you can't do that in PHP." Is
> > >>that the answer?
> > >
> > >You can do anything you want... :)
> >
> > Rob:
> >
> > Just to show that your words of wisdom don't go unnoticed, see here:
> >
> > http://php1.net/oop/strict/
> >
> > That's pretty slick.
> >
> > Now if I only understood WT* you did, I would be a better programmer for it.
>
> I used the magic methods __set() and __get() to facilitate error
> handling. These methods fire when a property is not accessible. So when
> the property does not exist these will fire. So first we init the
> properties when we create an instance (I'll discuss this shortly)... to
> init them we are setting them, this fires the __set() method... but
> since we check the value of the private ___init property, we don't fire
> an error in this instance. Future requests for the properties will not
> incurr a hit to __set() or __get() as long as the property is defined.
> If it is not defined then voila, the error fires and we die (we could do
> anything such as fire an exception but I just die for the example).
>
> With respect to initialization I use func_get_args() so that an
> arbitrary number of parameters can be passed to the constructor. This is
> useful since we don't know the number of properties. I also check if an
> argument is an array. If it is an array then I treat it as a property
> with an initialized value and create and set the property accordingly.
>
> That's all there is to it.
Oh, I didn't bother, but if someone finds this at all useful, they could
use debug_backtrace() to walk the call stack and output the actual
source file and line number where the illegal property was requested.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP