0

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();

Edited by stevenbaron1: typo correction

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by stevenbaron1
0

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();
$myRecords->getAllRecords();

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

Hope this helps,
darkagn

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.