<?php class MyObj { public static $instance = 0; public function myMethod() { self::$instance += 2; echo self::$instance . '<br/>'; } } class MyOtherObj extends MyObj { public static $instance = 0; public function myOtherMethod() { echo parent::$instance . '<br/>'; // parent 指代父类 echo self::$instance . '<br/>'; // self 表示本身类 } } $instance1 = new MyObj(); $instance1->myMethod(); // 2 $instance2 = new MyObj(); $instance2->myMethod(); // 4 $instance3 = new MyOtherObj(); $instance3->myOtherMethod(); // 4,0
function getId() { return $this->id; } function setName($name) { $this->name = $name; } function getName() { return $this->name; } } //---- variables --------------------------------------------------------------- $JSON = new JSON(); $myObj = new MyObj(); //---- logic ------------------------------------------------------------------- /* initialize object */ $myObj->setId('�l' . chr(18) . "ie\nn"); $myObj->setName('objectName'); array_push($myObj->attribs, '�first'); array_push($myObj->attribs, 'second'); array_push($myObj->attribs, 3); /* create JSON representation */ $jsonStr = $JSON->stringify($myObj); //---- clean-up ---------------------------------------------------------------- //---- content ----------------------------------------------------------------- ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>JSON parser test</title>