码迷,mamicode.com
首页 > Web开发 > 详细

PHP面向对象编程

时间:2015-04-28 18:45:02      阅读:134      评论:0      收藏:0      [点我收藏+]

标签: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;

?>

PHP面向对象编程

标签:php面向对象

原文地址:http://5618698.blog.51cto.com/5608698/1639852

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!