Static or non-static?

What is better to use in this context, static methods or a simple public method and always call them like this: $ request = new Request (); if ($ request-> isPostRequest ()) {do smth} of course, it’s easier to use static ones, but what is more correct to use?

Class Request { public static function isSecureConnection() {} public static function isPostRequest() {} public static function isAjaxRequest() {} ...etc } 
+7
oop php
source share
5 answers

If each Request is a true entity, then it would be better to use non-static elements. But if this is not the case and the methods are used in general, like the Sinus function in mathematics, then it would be better to be static.

In general, it would be better to declare static functions in a class consisting only of functions and without data elements.

+6
source share

You should always create a class as if it were used in a non-static environment.

Then you can use it as a Singleton with a lazy instantiation. Or even as an instance of the Static class. Or even as a standalone instance of an object. You later decide what to do with it.

If you start by declaring all members to be static, you basically just cover a bunch of global variables inside an illustrious namespace known as a class. You also statically allocate the memory used by this class, even if you do not call it or use it in your code.

So, just write as a class, then you decide what to do with it. Static / Non-Static. Singleton / Instance. Factory Sample or not. Allocator X / DLL memory is linked or something else.

The only exception is the static members used for accounting with respect to instances of objects; things like reference counting, caches and the like. This is a good thing about static / non-static, you can mix and match some smart actions.

What if later you need another request? Or what happens if you can create multiple ones because you are in a multi-threaded environment? Things will become really strange once you go this route with static.

+1
source share

It looks like you are processing one specific request.

Now this hints that you should make it single and / or use static functions.

Cause:

There is only one request, all state is determined by the environment passed to the process. Class methods are just helper functions, and you want to use them without an instance of the class.

On the other hand, you rarely used static functions with classes that are one of many, for example, one user in many, one of many questions.

+1
source share

Declaring class properties or methods as static makes them available without having to instantiate the class. A property declared as static cannot be accessed using an instance of a class object (although a static method can be used).

Example:

 <?php class Foo { public static function aStaticMethod() { // ... } } Foo::aStaticMethod(); $classname = 'Foo'; $classname::aStaticMethod(); // As of PHP 5.3.0 ?> 

See the link Static keyword from php.net .

0
source share

If you can, use static methods by default. static methods execute faster than non-static copies.

0
source share

All Articles