Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why showing Fatal Error on static function

Tags:

php

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.

like image 786
Haren Sarma Avatar asked Jul 10 '12 20:07

Haren Sarma


People also ask

How to access static function PHP?

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.

What is static:: PHP?

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.

How to access static property in PHP?

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 .

How to define static variable in PHP?

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


1 Answers

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.

like image 72
Explosion Pills Avatar answered Oct 22 '22 13:10

Explosion Pills