Static Keyword
Posted by Raj
Static Keyword:
In this Article,I will explain how to use Static Keyword in PHP5.
Example:
<?php
class Box
{
static private $color;
function __construct($value)
{
if($value != "")
{
Box::$color = $value;
}
$this->getColor();
}
public function getColor ()
{
echo Box::$color;
}
}
$a = new Box("RED");
$a = new Box("GREEN");
$a = new Box("");
?>
OUTPUT:
RED
GREEN
GREEN
In this Article,I will explain how to use Static Keyword in PHP5.
- To implement static keyword functionality to the attributes or the methods will have to be prefix with static keyword.
- Static properties or methods can be accessible without needing an instantiation of the class.
- A property declared as static can not be accessed with an instantiated class object.
- $this is not available inside the method declared as static.
- Static properties cannot be accessed using the arrow operator ->.
- Static properties can be accessed using the Scope Resolution Operator (::) operator.
Example:
<?php
class Box
{
static private $color;
function __construct($value)
{
if($value != "")
{
Box::$color = $value;
}
$this->getColor();
}
public function getColor ()
{
echo Box::$color;
}
}
$a = new Box("RED");
$a = new Box("GREEN");
$a = new Box("");
?>
OUTPUT:
RED
GREEN
GREEN
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
OOP interview questions and answers,
PHP,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.