php 基础笔记 - class

/***************************by garcon1986********************************/

 

<?php //简单示例 class SimpleClass { public $var = 'a default value'; public function displayVar(){ echo $this->var; } } // create an object创建一个对象 $A = new SimpleClass; //调用方法 $A -> displayVar(); echo '<p>'; //example2 class A{ function sjg(){ if(isset($this)){ echo '$this is defined.<br>/n'; echo get_class($this); //返回对象所属的类的名字 echo ")<br>"; }else { echo "/$this is not defined.<br> /n"; } } } class B{ function bar(){ A::sjg(); } } $a = new A(); $a -> sjg(); A::sjg(); $b = new B(); $b -> bar(); B::bar(); echo '<p>'; //example3 //创建一个实例 $instance = new SimpleClass(); //对象赋值 $assigned = $instance; $reference =& $instance; $instance->var = '$assigned will have this value'; $instance = null; var_dump($instance); echo "<br>"; var_dump($reference); echo "<br>"; var_dump($assigned); echo "<p>"; //example4 //继承 class ExtendClass extends SimpleClass{ //redefine the parent method function displayVar(){ echo "Extending class/n"; parent::displayVar(); } } $extended = new ExtendClass(); $extended->displayVar(); echo '<p>'; //autoload自动加载 function __autoload($class_name){ require_once $class_name.'.php'; } //$obj = new MyClass1(); //$obj = new MyClass2(); //constructors class BaseClass{ function __construct(){ print "In BaseClass constructor!/n"; } } class SubClass extends BaseClass{ function __construct(){ parent::__construct(); print "in SubClass constructor/n"; } } $obj = new BaseClass(); echo '<br>'; $obj->__construct(); echo '<br>'; $obj = new SubClass(); echo '<br>'; $obj->__construct(); echo '<p>'; //destructors class MyDestructableClass{ function __construct(){ print "In constructor<br>/n"; $this->name = "MyDestructableClass"; } function __destruct(){ print "Destroying ".$this->name.'<p>'; } } $obj = new MyDestructableClass(); //members visibility --- public, private, protected class MyClass{ public $public = 'public'; protected $protected = 'protected'; private $private = 'private'; function printHello(){ echo $this->public.'<br>'; echo $this->protected.'<br>'; echo $this->private.'<br>'; } } $obj = new MyClass(); echo $obj->public.'<br>'; //works //echo $obj->protected; //fatal error //echo $obj->private; //fatal error $obj->printHello(); //show public, protected, private echo '<br>'; class MyClass2 extends MyClass{ protected $protected = 'protected2'; function printHello(){ echo $this->public.'<br>'; echo $this->protected.'<br>'; //echo $this->private.'<br>'; } } $obj2 = new MyClass2(); echo $obj2->public.'<br>'; //works //echo $obj2->private; //undefined //echo $obj2->protected; //fatal error $obj2->printHello(); // show public, protected, undefined echo "<p>"; //method visibility 方法可见性 class MyClass3{ //constructors must be public public function __construct(){ } //declare a public method public function MyPublic(){ echo 'public'; } //declare a protected method protected function MyProtected(){echo 'protected';} //declare a private method private function MyPrivate(){echo 'private'; } //This is public function Foo3(){ $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass3 = new MyClass3; //$myclass3->MyPublic(); //$myclass3->MyProtected(); //error //$myclass3->MyPrivate(); //error $myclass3->Foo3(); echo '<br>'; class MyClass4 extends MyClass3 { function Foo4(){ $this->MyPublic(); $this->MyProtected(); //$this->MyPrivate(); //error } } $myclass4 = new MyClass4; //$myclass4->MyPublic(); $myclass4->Foo4(); ?>

 

 

你可能感兴趣的:(PHP,function,Class,reference,Constructor,autoload)