note 109717 added to language.oop5.static
| From: | nanhe dot kumar at gmail dot com | Date: | Tue, 14 Aug 2012 05:42:58 +0000 |
| Subject: | note 109717 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190059@lists.php.net to get a copy of this message | ||
<?php
/*
* Static method can call normal function can't assing value of normal they can only access
the static member or method
* Normal function can call static method through this but through this we can't access
static variable
* static variable and method are not member of object they are member of class
* I have written possible way to call or interaction normal to static
*/
?>
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
?>
<?php
class StaticTest{
public $a;
public static $b;
public function __construct() {
$this->a=0;
static::$b=0;
self::$b=0;
StaticTest::$b=0;
}
public function setB($b){
StaticTest::$b=$b;
//self::$b=$b;
//static::$b=$b;
}
public function setA($a){
$this->a=$a;
}
public function AB($a,$b){
$this->setA($a);
$this->setB($b);
}
public function getA(){
return $this->a;
}
public function getB(){
/**
* You can use any way
return self::$b;
return static::$b;
return StaticTest::$b;
*
*/
return self::$b;
}
public static function staticB()
{
return static::$b;
}
public static function staticAB(){
//You can not call non static memember and method in static function
// you can not $this keyword in static function
//return static::staticB(); //valid
//return self::staticB();// valid
//return StaticTest::staticB();// valid
return self::$b;//valid
}
public static function defaultAB(){
static::$b=10;
}
public function normalToStatic(){
echo $this->getA();
$this->defaultAB();
echo self::$b;
echo StaticTest::staticB();
}
public static function staticToNormal(){
$t=new StaticTest();
$t->normalToStatic();
}
}
?>
<?php
$t=new StaticTest();
$t->setB(4);
echo $t->getB();
echo $t->staticAB();
$t->normalToStatic();
StaticTest::staticToNormal();
$t::staticToNormal();
$x="StaticTest";
$x::staticToNormal();
//Final output 4401010010100101001010
?>
----
Server IP: 69.147.83.197
Probable Submitter: 210.7.74.130
----
Manual Page -- http://www.php.net/manual/en/language.oop5.static.php
Edit -- https://master.php.net/note/edit/109717
Del: integrated -- https://master.php.net/note/delete/109717/integrated
Del: useless -- https://master.php.net/note/delete/109717/useless
Del: bad code -- https://master.php.net/note/delete/109717/bad+code
Del: spam -- https://master.php.net/note/delete/109717/spam
Del: non-english -- https://master.php.net/note/delete/109717/non-english
Del: in docs -- https://master.php.net/note/delete/109717/in+docs
Del: other reasons-- https://master.php.net/note/delete/109717
Reject -- https://master.php.net/note/reject/109717
Search -- https://master.php.net/manage/user-notes.php