Re: Static vs. Instantiated Objects in PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It was helpful. I agree about programming to what is defined and declared in a language, but experience has taught that some methods are better than others (i.e. joining an array vs string concatenation in javascript). So I mostly just wanted to convince myself that static was sane in 5, and what better way than conferring with those more familiar with it :-)

Thanks,
Mark

On Apr 20, 2006, at 7:06 PM, Anas Mughal wrote:

I would not worry about internal implementation of PHP5. (Who knows, they may change things around a little bit in PHP6!)

In general, program to what is defined and declared in a language. That is our contract with the Zend engine.

Remember, static method calls are resolved at compile time. Moreover, there is no need to instantiate objects of the class in different places in order to call methods on it. I use/access my utility objects within other objects. So, instead of passing them around or instantiating instances whenever I need to call a method, I would just declare those methods as static.

Hope this helps.





On 4/20/06, Mark Baldwin <mark.baldwin@xxxxxxxxxxx> wrote:
Well, that's part of the reason I'm asking. I'm not sure, and here's why:

Each time you call a static method is it acting as just a function pointer or does PHP go through a lightweight version of object creation each time you access the object. If it's just a function pointer, why bother with the object oriented syntax? You gain nothing from it except carpal-tunnel.

However, if PHP has to either parse the object or goes through some kind of process each time you access the static method, then the obvious choice is to take the hit of instantiating an object and be able to call methods as much as you'd want for cheap.

But not knowing the internals of the engine, and not being able to find much on the topic on the web, I'm left to asking the list.

Do you have any insight on it?



On Apr 20, 2006, at 2:47 PM, Anas Mughal wrote:

Mark,
What would you think?


On 4/20/06, Mark Baldwin < mark.baldwin@xxxxxxxxxxx> wrote:
Greetings all,

Does anyone know how static objects work behind the scenes in PHP5?
More specifically, is there a benefit to declaring an object and its
methods as static vs the more traditional OO way of instantiating an
object and then calling methods through the -> operator?

For example if I have:

class Example {
        public static function doSomething() {
                /* doing something here */
        }
}

and

class Example {

        private __construct() {
                /* constructing here */
        }

        public function doSomething() {
                /* doing something here */
        }

}

Is it more/less/equally efficient for the engine and parser to do:
$result = Example::doSomething();

or

$example = new Example();
$result = $example->doSomething();

Thanks in advance,
Mark

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




--
Anas Mughal




--
Anas Mughal


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux