标签:
/*
class ren
{
    public static $color;//静态
    static function ()
    {
        ren::$color;
        self::$color;//self只能写在类里面,代表这分类
    }
}
*/
//不能实例化的类:抽象类
abstract class Animal	//关键字abstract
{
    public $dong;
    public $jiao;
    function chi()
    {
    }
    function shui()
    {
    }
}
class Ren extends Animal
{
}
$d=new Ren();
var_dump($d);
//接口:极度抽象的类,里面的方法要重写
//关键字 interface
//里面的方法没有函数体
//实现接口使用的关键字是implements,不是extends
//实现接口的子类必须要实现接口中的每个方法
interface Animal2   //不要加class
{
}
class Ren2 extends Animal2
{
}
//实例
interface USB
{
    function Read();
    function Write();
}
class Mouse implements USB
{
    function Read()
    {
        echo "插入了鼠标";
    }
    function Write()
    {
        echo "通电给鼠标";
    }
}
$m=new Mouse();
$m->read();
$m->Write();
//析构方法:在对象销毁之前,将内存释放,链接关闭and so on
class ren
{
    public $name;
    //析构方法
    function __destruct()
    {
     echo "该对象销毁了";
    }
}
//写法特殊:__destruct
//执行时间特殊,销毁时执行
$r=new ren();
$r->name="张丹";
var_dump($r);
//tostring可以在输出对象时候调用,必须有一个返回值
class ren
{
    public $name;
    public $sex;
    public $age;
    function run()
    {
    }
    function show()
    {
        echo "name代表姓名,sex代表性别,age代表年龄";
    }
    function __tostring()
    {
        //  return "name代表姓名,sex代表性别,age代表年龄";
        return $this->name;
    }
}
$r=new ren();
$r->show();
echo $r;
//其他小知识点
$a=10;
$b=20;
$c=25;
if(!isset($b))		//判断是否存在
{
    echo "变量b不存在";
    exit;//退出程序
    die("变量B不存在!");//输出错误信息并且退出程序
}
echo $a+$b;
echo $a*$b;
//输出数组
$attr=array(1,2,3,4);
var_dump($attr);
print_r($attr);
echo "hello","haha";//可以输出多个字符串
print"helo";//只能输出一个
//加载类 命名:ren.class.php
class ren
{
    public $name;
    public $sex;
    public function say()
    {
        echo "hello";
    }
}
//引用类,加载类
include("ren.class.php");
require_once("ren.class.php");//写在文件顶端,如果出现错误,代码停止执行
require_once"ren.class.php";
    //自动加载项
    //1.所有的类的文件命名,要求使用同一个规则
    //2.文件名里面必须有类名
    //3.所有类文件必须在同一个文件夹下
    //用的很少,
function __autolode($classname)
{
    require $classname.".class.php";
}
0607am抽象类&接口&析构方法&tostring&小知识点
标签:
原文地址:http://www.cnblogs.com/pangchunlei/p/5575891.html