note 109717 deleted from language.oop5.static by danbrown
| From: | danbrown@php.net | Date: | Fri, 31 Aug 2012 16:33:41 +0000 |
| Subject: | note 109717 deleted from language.oop5.static by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190385@lists.php.net to get a copy of this message | ||
Note Submitter: nanhe dot kumar at gmail dot com
----
<?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
?>