Jump to content

static keyword and scope resolution problem


Go to solution Solved by requinix,

Recommended Posts

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 ??

 

  • Solution

static::$test only works if you have PHP 5.3+. If you don't then it will definitely create an error.

 

Scope resolution ( ::) - this is used to call constants and static variables, static functions and classes but not with instance/object ??

1. Static things (constants, static variables, static methods)

2. Used with self, parent, and static keywords and even for instance methods (eg, parent::__construct)

 

static keyword - we can call static variable and functions without  the need of instantiation i.e directly  using scope resolution....

Yes: static variables, static methods, and with PHP 5.3+ the statically-called class.

 

the class also becomes static , if any property  or method declared static in it

No. PHP does not have static classes. You could call a class "static" if you so wanted by marking it final and making the constructor private but it's not a true static class.

 

After inheriting from such class

It's not true inheritance: self:: and parent:: don't respect the inheritance hierarchy (static:: does) and you don't have to respect the parent method's signature.

 

if we override property  or method we have to use static keyword again while override,,,,

Yes because they're static. Can't create a new non-static property/method with the same name. Edited by requinix

Just because Dreamweaver says it is an error, doesn't mean it is. Since the syntax is relatively new, your version of Dreamweaver may just not recognize it. You need to actually run the PHP file and see if you get an error from PHP.

Edited by kicken
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.