PHP面向对象编程

1.全局变量

<?php

class MyClass{

static $myStaticVariable=0;

function myMethod(){

print self::$myStaticVariable;

}

}

 

$obj=new MyClass();

$obj->myMethod();

?>

2.全局变量改变

<?php

class MyClass{

static $myStaticVariable=0;

public $uniqueId;

function __construct(){

self::$myStaticVariable++;

$this->uniqueId=self::$myStaticVariable;

}

}

 

$obj1=new MyClass();

print $obj1->uniqueId." ";

$obj2=new MyClass();

print $obj2->uniqueId;

?>

 

3.静态方法

<?php

class MyClass{

static function printHelloWorld(){

print "hello world ";

self::printMe();

}

 

static function printMe(){

print " zhuchengdie";

}

}

MyClass::printHelloWorld();

 

?>

 

4.继承

<?php

class MyClass{

static function printHelloWorld(){

print "hello world ";

self::printMe();

}

 

static function printMe(){

print " tony";

}

}

class Child extends MyClass{

function __construct(){

parent::printHelloWorld();

}

}

 

$child=new Child();

 

?>

 

5.枚举

<?php

class MyClass{

const RED="Red";

const GREEN="Green";

const BLUE="Blue";

function printBlue(){

print self::BLUE;

}

}

print MyClass::GREEN;

$obj=new MyClass();

$obj->printBlue();

 

?>

6.对象的引用

<?php

class MyClass{

public $var=1;

}

$obj1=new MyClass();

$obj2=$obj1;

$obj2->var=2;

print $obj1->var;

?>

打印2

7.克隆

<?php

class MyClass{

public $var=1;

}

$obj1=new MyClass();

$obj2=clone $obj1;

$obj2->var=2;

print $obj1->var;

?>

打印1

 

8.多态

<?php

class Cat{

function miao(){

print "miao";

}

}

class Dog{

function wangwang(){

print "wangwang";

}

}

 

function printRightSound($obj){

if($obj instanceof Cat){

$obj->miao();

}else if($obj instanceof Dog){

$obj->wangwang();

}else{

print "Error comes";

}

print "<br>";

}

printRightSound(new Cat());

printRightSound(new Dog());

?>

9.多态与继承

<?php

class

Animal{

function makeSound(){

print "I‘ m other‘s father";

}

}

 

class

Cat extends Animal{

function makeSound(){

print "miao";

}

}

class

Dog extends Animal{

function makeSound(){

print "wangwang";

}

}

 

function

printRightSound($obj){

if($obj instanceof Animal){

$obj->makeSound();

}else{

print "Error comes";

}

print "<br>";

}

printRightSound(new Cat());

printRightSound(new Dog());

?>

10.parent::和self::

<?php

class

.Ancestor{

const NAME="Ancestor";

function __construct(){

print "In ".self::NAME." constructor<br>";

}

}

 

class

Child extends Ancestor{

const NAME="Child";

function __construct(){

parent::__construct();

print "In ".self::NAME." constructor";

}

}

$obj=new Child();

?>

11.抽象

<?php

abstract class

MyClass{

abstract function draw();

}

class

Square extends MyClass{

function draw(){

 

}

}

class

Circle extends MyClass{

function draw(){

 

}

}

?>

12.接口

<?php

interface

MyClass{

function draw();

}

class

Square implements MyClass{

function draw(){

 

}

}

class

Circle implements MyClass{

function draw(){

 

}

}

但是接口是允许多重继承的:

interface

No1 extends No2,No3,…{}

与类实现接口类似,一个接口只能继承与自己互相不冲突的接口(也就是说,如果No2定义了No1已经定义的方法或者常量,我们将会收到报错信息)。

?>

13.toString()方法

<?php

class

MyClass{

function __construct($name){

$this->name=$name;

}

private $name;

 

function __toString(){

return $this->name;

}

 

}

$obj=new MyClass("tony");

print

$obj;

?>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。