php - Notice: Use of undefined constant self - assumed 'self' , When put in property_exists as the first argument -
i'm trying use self
instead of typing class name inside propery_exists
function follows :
private static function instantiate($record){ $user = new self; foreach($record $name => $value){ if(isset($user->$name) || property_exists(self, $name)){ $user->$name = $value; } } return $user; }
but when ran script error :
notice: use of undefined constant self - assumed 'self' in /var/www/photo_gallery/includes/user.php on line 36
line 36 line property_exists
method called.
when change self user
(the class name). works perfectly.
i want know why using self
giving such notice ? doesn't self
refer class?
use self
refer current class. not class name.
try using magic constants:
if(isset($user->$name) || property_exists(__class__, $name)){
from php manual: __class__
the class name. (added in php 4.3.0) of php 5 constant returns class name declared (case-sensitive). in php 4 value lowercased. class name includes namespace declared in (e.g. foo\bar). note of php 5.4 class works in traits. when used in trait method, class name of class trait used in.
an example:
class test { public function __construct(){ echo __class__; } } $test = new test();
output:
test
Comments
Post a Comment