I have a tiny class of inserting payment record into database. below is given:
<?php
class pay{
public static function pay($user, $income, $type, $refid='--'){
mysql_query("INSERT INTO earn VALUES (NULL, '$user', '$income', '$refid', '$type', ".time().")");
}
}
?>
But browser gives the below fatal error..
Fatal error: Constructor pay::pay() cannot be static in F:\xampp\htdocs\new\sts\class.php on line 41
I am confused that why the error occurring.. please help me to understand.
Any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an instance of the class. They are permitted to access only static methods and static variables. To add a static method to the class, static keyword is used.
Definition and Usage The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ). print $foo::$my_static .
You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name. <? php function keep_track() { STATIC $count = 0; $count++; print $count; print "<br />"; } keep_track(); keep_track(); keep_track(); ?>
If you have a method name that is the same as the name of the class, it is considered to be a constructor. Constructors cannot be static. You must either rename this class or method, or make the method not static and create an instance of the class when you want to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With