Hi Guys,

I noticed that in some apps people tend to use the following method in classes without instantiating an instance or defining objects, but this works, could someone explain whether this is a correct way of doing it. (its for a simple data management and interface app)

For eg. In the model:

class getRecords {

     public function getAllRecords () 
     //get records from db



In the controller:

$records = getRecords::getAllRecords();

Recommended Answers

All 2 Replies

This is a static call to your function, which is fine if there is no calls to member variables or functions in the getAllRecords function (ie using $this). From a relatively early version of PHP (around PHP4 from memory) they introduced the static keyword which was supposed to be used to mark such functions as being able to be called in the manner you describe, but it was only intended as a guide and not enforced at runtime.

In general, non-static calls to functions go like this:

$myRecords = new getRecords();

That is, you instantiate an object of your class and then call the function that you need.

Hope this helps,

Hey Darkagn! Thanks a lot for clarifying that! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.