{ return $input * $input; } } // -- test creating of the instance of ClassName $a = new Classname("First"); $a = new Classname("Second"); $a = new Classname("Third"); $a->attribute = "value"; echo $a->attribute; $a->private_attribute = 5; echo $a->private_attribute; $a = new A(); $a->operation(); $b = new B(); $b->operation(); // ---------------------------------------------- // reflection - check types and instanceof // ---------------------------------------------- // high effecient instanceof methods // // - note on how to convert a boolean to string echo "{\$b instanceof B}" . ['false', 'true'][$b instanceof B] . "\n"; echo "{\$b instanceof A}" . ['false', 'true'][$b instanceof A] . "\n"; echo "{\$b instanceof Displayable}" . ['false', 'true'][$b instanceof Displayable] . "\n"; // ---------------------------------------------- // type hint -use of instanceof // ---------------------------------------------- // - uncomment the statements to uncover the error. /*function check_hint(B $something) {
public $test1 = 'PHP'; public $test2 = 'Java'; public $test3 = 'C++'; } $a = new A(); foreach ($a as $output) { echo $output . '<br />'; } //类的继承关系 class B { public $test = "very large"; function operation() { echo "welcome.<br /><br />"; echo "The value of \$test is {$this->test}. <br /><br />"; } } class C extends B { public $test = "perfect"; function operation() { echo "welcome back <br /><br />"; echo "The value of \$test is {$this->test} <br /><br />"; } } $a = new B(); $a->operation(); $b = new C(); $b->operation();