php面向对象编程快速入门
面向对象
把数据及对数据的操作方法放在一起,作为一个相互依存的整体就是对象。对同类对象抽象出其共性,形成类。
class声明类
<?php
//class声明类
class Student{
public $name = '张三';
public function print(){
echo $this->name;
}
}
$student = new Student();
$student->print();
new 创建对象
<?php
//class声明类
class Student{
public $name = '张三';
public function print(){
echo $this->name;
}
}
//new 创建对象
$student = new Student();
$student->print();
属性
<?php
//class声明类
class Student{
//定义公有属性
public $name = '张三';
//定义私有属性
private $age = 25;
//定义受保护属性
protected $money = 1000;
//私有属性,类内部使用
public function getAge(){
return $this->age;
}
//受保护属性,本类使用,子类不能使用
public function getMoney(){
return $this->money;
}
}
//new 创建对象
$student = new Student();
//获取公有属性
echo $student->name;
//获取私有属性
echo $student->getAge();
//获取受保护属性
echo $student->getMoney(); private $price = '100000';
}
方法
<?php
//class声明类
class Student{
//定义公有属性
public $name = '张三';
//定义私有属性
private $age = 25;
//定义受保护属性
protected $money = 1000;
//私有属性,类内部使用
public function getName(){
return $this->name;
}
//私有方法
private function getAge(){
return $this->age;
}
//受保护方法
protected function getMoney(){
return $this->money;
}
//类内部调用私有方法和受保护方法
public function print(){
echo $this->getAge();
echo $this->getMoney();
}
}
//new 创建对象
$student = new Student();
//获取公有属性
echo $student->getName();
//获取私有属性
$student->print();
访问修饰符
public private protected
<?php
//class声明类
class Student{
//定义公有属性
public $name = '张三';
//定义私有属性
private $age = 25;
//定义受保护属性
protected $money = 1000;
//私有属性,类内部使用
public function getName(){
return $this->name;
}
//私有方法
private function getAge(){
return $this->age;
}
//受保护方法
protected function getMoney(){
return $this->money;
}
//类内部调用私有方法和受保护方法
public function print(){
echo $this->getAge();
echo $this->getMoney();
}
}
//new 创建对象
$student = new Student();
//获取公有属性
echo $student->getName();
//获取私有属性
$student->print();
this关键字
<?php
//class声明类
class Student{
//定义公有属性
public $name = '张三';
//定义私有属性
private $age = 25;
//定义受保护属性
protected $money = 1000;
//私有属性,类内部使用
public function getName(){
//代表当前类的对象
return $this->name;
}
//私有方法
private function getAge(){
//this代表当前类的对象
return $this->age;
}
//受保护方法
protected function getMoney(){
return $this->money;
}
//类内部调用私有方法和受保护方法
public function print(){
echo $this->getAge();
echo $this->getMoney();
}
}
//new 创建对象
$student = new Student();
//获取公有属性
echo $student->getName();
//获取私有属性
$student->print();
构造方法
<?php
//class声明类
class Student{
public $name;
public $age;
//构造方法
public function __construct($name,$age)
{
$this->name = $name;
$this->age = $age;
}
}
//new 创建对象
$student = new Student('张三', 23);
//获取name
echo $student->name;
//获取age
echo $student->age;
析构方法
<?php
//class声明类
class Student{
public $name;
public $age;
//构造方法
public function __construct($name,$age)
{
$this->name = $name;
$this->age = $age;
}
//析构方法
public function __destruct()
{
echo '析构方法...';
}
}
//new 创建对象
$student = new Student('张三', 23);
//获取name
echo $student->name;
//获取age
echo $student->age;
static 关键字
<?php
//class声明类
class Student{
//静态属性
public static $name = '张三';
//静态方法
public static function print(){
//类内部访问静态方法
echo self::$name;
}
}
//访问类的静态属性
echo Student::$name;
//访问类的静态方法
Student::print();
self 关键字
<?php
//class声明类
class Student{
//静态属性
public static $name = '张三';
//静态方法
public static function print(){
//类内部访问静态方法
echo self::$name;
}
}
//访问类的静态属性
echo Student::$name;
//访问类的静态方法
Student::print();
const 关键字
<?php
//class声明类
class Student{
//定义类常量
const ADDRESS = '广东省广州市';
//方法访问类常量
public function getAddress(){
return self::ADDRESS;
}
}
//访问类常量
echo Student::ADDRESS;
//访问类的方法里的常量
$student = new Student();
echo $student->getAddress();
extends 关键字
<?php
//父类
class Man{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
//子类,extends继承Man父类
class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
//打印属性
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
}
//创建对象,访问打印方法
$student = new Student('张三',25,8);
$student->print();
抽象类
<?php
//抽象类
abstract class Human
{
//抽象属性
public $name;
public $age;
//抽象方法
abstract public function print();
}
//父类
class Man extends Human {
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
//实现父类抽象方法
public function print(){
echo $this->name;
echo $this->age;
}
}
//子类,extends继承Man父类
class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
//重写父类方法
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
}
//创建对象,访问打印方法
$student = new Student('张三',25,8);
$student->print();
abstract关键字
<?php
//抽象类
abstract class Human
{
//抽象属性
public $name;
public $age;
//抽象方法
abstract public function print();
}
//父类
class Man extends Human {
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
//实现父类抽象方法
public function print(){
echo $this->name;
echo $this->age;
}
}
//子类,extends继承Man父类
class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
//重写父类方法
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
}
//创建对象,访问打印方法
$student = new Student('张三',25,8);
$student->print();
final 关键字
<?php
//父类
class Man{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
//final方法不能被子类重写
final public function print(){
echo $this->name;
echo $this->age;
}
}
//final类不能被继承
final class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
}
//创建对象,访问打印方法
$student = new Student('张三',25,8);
$student->print();
interface关键字和implements关键字
<?php
//Human接口
interface Human
{
//接口方法
public function print();
}
//父类 实现Human接口
class Man implements Human {
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
//实现接口方法
public function print(){
echo $this->name;
echo $this->age;
}
}
//final类不能被继承
final class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
//重写父类方法
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
}
//创建对象,访问打印方法
$student = new Student('张三',25,8);
$student->print();
自动加载类
student.class.php
<?php
//Human接口
interface Human
{
//接口方法
public function print();
}
//父类
class Man implements Human {
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
//实现接口方法
public function print(){
echo $this->name;
echo $this->age;
}
}
//final类不能被继承
final class Student extends Man{
//年级
public $grade;
public function __construct($name, $age, $grade)
{
parent::__construct($name, $age);
$this->grade = $grade;
}
//重写父类方法
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
}
index.php
<?php
function autoload($className){
$filename = "./". $className .".class.php";
include $filename;
}
//注册给定的函数作为 __autoload 的实现
spl_autoload_register('autoload');
//创建对象,访问打印方法
$student = new student('张三',25,8);
$student->print();
单例模式
<?php
class Student{
public $name;
public $age;
public $grade;
//私有属性,用于保存实例
private static $instance;
//构造方法私有化,防止外部创建实例
private function __construct(){}
public function print(){
echo $this->name;
echo $this->age;
echo $this->grade;
}
//克隆方法私有化,防止复制实例
private function __clone(){}
//公有方法,用于获取实例
public static function getInstance(){
//判断实例有无创建,没有的话创建实例并返回,有的话直接返回
if(!(self::$instance instanceof self)){
self::$instance = new self();
}
return self::$instance;
}
}
$student = Student::getInstance();
$student->name = '张三';
$student->age = 25;
$student->grade = 6;
$student->print();
工厂模式
<?php
//Man类
class Man{
public $name;
public $age;
public $sex = '男';
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function print(){
echo $this->name;
echo $this->age;
echo $this->sex;
}
}
//Woman类
class Woman{
public $name;
public $age;
public $sex = '女';
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function print(){
echo $this->name;
echo $this->age;
echo $this->sex;
}
}
//工厂类
class Factory
{
//工工厂类生产方法
public static function createObj($type,$name, $age)
{
$obj = null;
switch($type)
{
case 'woman':
$obj = new Woman($name,$age);
break;
case 'man':
$obj = new Man($name, $age);
break;
}
return $obj;
}
}
//生产类生产Woman
$woman = Factory::createObj('woman','小红',23);
$woman->print();
//生产类生产man
$man = Factory::createObj('man','张三',25);
$man->print();
- 上一篇 »JavaScript面向对象编程实战
- 下一篇 »基于PHP的cURL快速入门