Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

static keyword and scope resolution problem

$
0
0

i am learning static keyword.. and find this example , but it shows error ,, which i highlited below

 

<?php
class a{

static protected $test="class a";

public function static_test(){

echo static::$test; // Results class b             here the program shows error .... can we use static keyword to call the variable or we have to use a class name?
echo self::$test; // Results class a

}

}

class b extends a{

static protected $test="class b";

}

$obj = new b();
$obj->static_test();
?>

 

getting confused about these two terms.. i tell you what i understand till now about them... make me clear plz

 

Scope resolution (::) - this is used to call constants and static variables, static functions and classes but not with instance/object ?? am i right ?? anything more about it ?

 

static keyword -   we can call static variable and functions without  the need of instantiation i.e directly  using scope resolution.... the class also becomes static , if any property  or method declared static in it. After inheriting from such class  if we override property  or method we have to use static keyword again while override,,,, ok or i need to learn more ??

 


Viewing all articles
Browse latest Browse all 13200

Trending Articles