Viewed   86 times

What's the difference between these object callings?

Non Static:

$var = new Object;


$var = User::function();

And also inside a class why should I use the static property for functions?


static public function doSomething(){



Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed as you've shown without first creating an instance). In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.

Wednesday, October 26, 2022

Its as if it is executing in a totally different memory space and creating a new program, such that static member variables are only shared between same process as opposed to multiple web threads.`

Exactly! :) That's 100% how this works. Each PHP request is a new one, with its own memory. The static keyword is not designed to work around that.

If you want to persist stuff across multiple processes / requests in a web application, you need to use sessions.

Wednesday, October 5, 2022

Here is the rule:

A static method can be used in both static method and non-static method.

A non-static method can only be used in a non-static method.

Thursday, December 1, 2022

Late Static binding:

late static bindings work by storing the class named in the last "non-forwarding call".

try using static:: keyword instead of self:: in your example.

Monday, September 19, 2022

In this case you have to go with example 2, because what you're trying to do in example 1 will not work:

$food = new Food( "apple", "Nice juicy Pink Lady apple" );
Food::getAllFood(); //static

There will not be any food returned unless there's some hardcoded in the class. What you put in with the constructor, you put into the instance in $food. But you're calling the class itself Food::getAllFood() to retrieve. That doesn't make sense.

Are there cases where it would make sense to include a static method in a class? Yes. Like if I was making a db connection class that would hold a connection, but I also wanted to expose some date format methods that are related to the particular DBMS but don't actually require the connection to the db to be active. Anything touching the db would have to be non-static, but some transformation methods on data I've already pulled out could be static:

$db = new JDE_DBClass($connectionString);
$rows = $db->doSelectQuery("select * from whatever");
$date = JDE_DBClass::convertJDE_Date_to_PHP_Date($rows[0]['dateField']);

In this case you might want to do this to allow conversion without having to instantiate the db object, because perhaps you might need to convert back and forth between JDE's so-called Julian format and regular dates before even determining whether you need to connect to the db:

$date = JDE_DBClass::convertJDE_Date_to_PHP_Date('114309');
Saturday, November 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :