Why is the function getUserID in the User class defined as static?
The function getUserID in the User class is defined as static because it does not rely on any instance-specific data or properties of the User class. By defining it as static, it can be called without needing to create an instance of the User class, making it more convenient for accessing the user ID.
class User {
private static $userID = 123;
public static function getUserID() {
return self::$userID;
}
}
// To call the getUserID function
$userID = User::getUserID();
echo $userID;
Keywords
Related Questions
- How can the use of getimagesize() and $_FILES['file']['tmp_name'] be optimized for efficient image processing in PHP?
- What is the function of arsort() in PHP and how does it differ from asort()?
- How can PHP developers effectively manage and monitor email functionality to prevent abuse and ensure compliance with hosting provider policies?