php-general Digest 12 Jun 2011 19:01:17 -0000 Issue 7354
Topics (messages 313490 through 313492):
Re: Class not used as an object (Scope Resolution Operator)
313490 by: Paul M Foster
313491 by: David Harkness
execute my php code before every php call
313492 by: techloop
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On Thu, Jun 09, 2011 at 03:42:49PM -0600, George Langley wrote:
> Hi all. Am fixing some inherited code, and the previous coder created a
> class, ie:
>
> class myClass {
> function &doThis($passedVar) {
> doSomething;
> }
>
> function &doThat($anotherVar) {
> doSomethingElse;
> }
> }
>
> BUT, I don't see anywhere where he created an object, ie:
>
> $myObject = new myClass();
>
> or
>
> $myObject = myClass::doThis("value");
>
> Instead, it's only ever just called directly with a "Scope Resolution
> Operator", ie:
>
> myClass::doThis("valueOne");
> myClass::doThat($whatever);
> myClass::doThis("valueTwo");
> myClass::doThat($andSoOn);
>
> It seems that this would be making an object, and then destroying it
> again, on each of the four calls above, which I would think would be wasteful
> - time, memory, cpu usage, etc.
> The class has no constants or variables (properties) for any need for
> persistence, and is just a collection of functions (methods), so I don't see
> a reason to group them into a class - they could all reside as independent
> functions within the php file.
> Is this good? Is there some advantage to making a non-persistent class?
> Thanks!
I'm not real good with static stuff, but he's essentially using calls to
what are [being treated as] static methods inside the class. I'm not
sure this means an actual object is being created. There's certainly no
reason to create one.
If these methods truly have no persistent members within the class, then
I agree, they can simply be external functions. These may be grouped
this way for logical reasons, but if I have functions like this (which
relate to a class but don't depend on the class), I generally put them
in the class source file but outside the class. They become global as
soon as the source file is include()ed.
Paul
--
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com
--- End Message ---
--- Begin Message ---
All of the above with a clarification: they are instance methods which you
are calling statically. The object is not instantiated in this case. PHP
allows this but will issue an E_STRICT warning. To remove the warning just
add "static" before each of them, assuming they are only ever called
statically:
class myClass {
static function &doThis($passedVar) {
doSomething;
}
static function &doThat($anotherVar) {
doSomethingElse;
}
}
David
--- End Message ---
--- Begin Message ---
Hi all,
I have a simple code that override some env vars but i need it to run before
any other php code on every php execution.
I would like to avoid any change of all php code (even if its only to
include the include command).
Any one knows of such way ?
I dont mind overriding this vars on php configuration file but i dont have
any control of it as its should work on even the most strict web hosting
(that still allow env vars manipulation)
Any alternatives ?
Thanks,
TL
--- End Message ---