时间:2021-07-01 10:21:17 帮助过:4人阅读
php
class Page
{
}
?>
php
class Page
{
// 2 attributes
var $attribute1 ;
var $attribute2 ;
// 2 functions
function Operation1()
{
}
function Operation2( $param1 , $param2 )
{
}
}
?>
1 php
2 class Page
3 {
4 // 2 attributes
5 var $attribute1 ;
6 var $attribute2 ;
7
8 // 2 functions
9 function Operation1()
10 {
11 }
12
13 function Operation2( $param1 , $param2 )
14 {
15 }
16
17 // construction function
18 function __construct( $param )
19 {
20 $this -> attribute1 = $param ;
21 }
22
23 // destruction function
24 function __destruct()
25 {
26 $this -> attribute1 = 0 ;
27 }
28 }
29 ?>
1 php
2 class Page
3 {
4 // 2 attributes
5 var $attribute1 ;
6 var $attribute2 ;
7
8 // 2 functions
9 function Operation1()
10 {
11 }
12
13 function Operation2( $param1 , $param2 )
14 {
15 }
16
17 // construction function
18 function __construct( $param )
19 {
20 $this -> attribute1 = $param ;
21 echo " It will construct the attribute: {$this->attribute1}
" ;
22 }
23
24 // destruction function
25 function __destruct()
26 {
27 $this -> attribute1 = 0 ;
28 }
29 }
30 $instance1 = new Page( " attribute1 in instance 1 " );
31 $instance2 = new Page( " attribute2 in instance 2 " );
32 $instance1 = new Page();
33 ?>
1 It will construct the attribute : attribute1 in instance 1
2 It will construct the attribute : attribute2 in instance 2
3 It will construct the attribute :
1 2 class Page
3 {
4 //2 attributes
5 var $attribute1;
6 var $attribute2;
7
8 //2 functions
9 function Operation1()
10 {
11 }
12
13 function Operation2($param1,$param2)
14 {
15 }
16
17 //construction function
18 function __construct($param)
19 {
20 $this->attribute1=$param;
21 echo "It will construct the attribute: {$this->attribute1}
";
22 }
23
24 //destruction function
25 function __destruct()
26 {
27 $this->attribute1=0;
28 }
29 function __set($name,$value)
30 {
31 if($name==="attribute2"&&$value>=2&&attribute2<=100)
32 {
33 $this->attribute2=$value;
34 }
35 }
36 function __get($name)
37 {
38 return $this->$name;
39 }
40 }
41 $instance1=new Page("attribute1 in instance 1");
42 $instance2=new Page("attribute1 in instance 1");
43 $instance3=new Page();
44 $instance3->attribute2=99;
45 echo $instance3->attribute2;
46 ?>
1 2 class Page
3 {
4 //2 attributes
5 public $attribute1;
6 public $attribute2;
7
8 //2 functions
9 public function Operation1()
10 {
11 }
12
13 public function Operation2($param1,$param2)
14 {
15 }
16
17 //construction function
18 public function __construct($param)
19 {
20 $this->attribute1=$param;
21 echo "It will construct the attribute: {$this->attribute1}
";
22 }
23
24 //destruction function
25 public function __destruct()
26 {
27 $this->attribute1=0;
28 }
29 public function __set($name,$value)
30 {
31 if($name==="attribute2"&&$value>=2&&attribute2<=100)
32 {
33 $this->attribute2=$value;
34 }
35 }
36 public function __get($name)
37 {
38 return $this->$name;
39 }
40 }
41 $instance1=new Page("attribute1 in instance 1");
42 $instance2=new Page("attribute1 in instance 1");
43 $instance3=new Page();
44 $instance3->attribute2=99;
45 echo $instance3->attribute2;
46 ?>
1 php
2 interface IDispose
3 {
4 function Dispose();
5 }
6
7 class page extends IDispose
8 {
9 function Dispose()
10 {
11 // Your code here.
12 }
13 }
14 ?>
1 2 class Math
3 {
4 const pi=3.1415927;
5 }
6 echo "Math::pi='.Math::pi."\n";
7 ?>
1 2 class Shape
3 {
4 static function SquredSize($length)
5 {
6 return $length*$length;
7 }
8 }
9 echo "Squre:".Shape::SquredSize(10);
10 ?>
$c = clone $b
如果你想改变$c的某一特性,怎么办?这个时候你需要重写__clone方法。
1 2 abstract class Shape
3 {
4 abstract protected function getSquared();
5 abstract protected function getLineCount();
6 public function printShapeValue()
7 {
8 echo "The squared size is :{$this->getSquared()}
";
9 echo "And the number of lines is :{$this->getLineCount()}
";
10 }
11 }
12
13 class Rectange extends Shape
14 {
15 protected function getSquared()
16 {
17 return 12*12;
18 }
19 protected function getLineCount()
20 {
21 return 4;
22 }
23 }
24
25 class Triangle extends Shape
26 {
27 protected function getSquared()
28 {
29 return 10*12;
30 }
31 protected function getLineCount()
32 {
33 return 3;
34 }
35 }
36
37 $rect=new Rectange();
38 $rect->printShapeValue();
39
40 $tri=new Triangle();
41 $tri->printShapeValue();
42 ?>
The squared size is : 144
And the number of lines is : 4
The squared size is : 120
And the number of lines is : 3
1 2 class Caller
3 {
4 private $x = array(1, 2, 3);
5
6 public function __call($m, $a)
7 {
8 print "Method $m called:\n";
9 var_dump($a);
10 return $this->x;
11 }
12 }
13
14 $foo = new Caller();
15 $a = $foo->test1(1, "2", 3.4, true);
16 var_dump($a);
17 ?>
Method test1 called : array ( 4 ) { [ 0 ] => int( 1 ) [ 1 ] => string ( 1 ) " 2 " [ 2 ] => float ( 3.4 ) [ 3 ] => bool( true ) } array ( 3 ) { [ 0 ] => int( 1 ) [ 1 ] => int( 2 ) [ 2 ] => int( 3 ) }
$foo->test1这个方法在foo类里不存在的,但是定义了__call之后,就可以了。 1 php
2 function __autoload( $class_name ) {
3 require_once $class_name . ' .php ' ;
4 }
5
6 $obj1 = new MyClass1();
7 $obj2 = new MyClass2();
8 ?>
1 php
2 class Page
3 {
4 public function __toString()
5 {
6 return ' Hello ' ;
7 }
8 }
9 $page = new Page();
10 echo $page ;
11 ?>
Hello
10. 反射。