zoukankan      html  css  js  c++  java
  • 自动加载类 (面向对象)----2017-04-20

    一、__tostring()方法(了解)

    写在类里;必须有返回值

    class Ren

    {

           public $name;

           public function __ tostring()

           {

                  return "该类是人类,name代表姓名";

           }

    }

    $r = new Ren();

    echo $r;

    二、__clone()方法(了解)

    class Ren

    {

           public $name="张三";

           //第二种修改成员变量值的方法

           public function __clone()

           {

                  $this->name = "李四";   ------this指复制的对象

           }

    }

    $r = new Ren();

    $c = clone $r;    ----克隆一个和$r = new Ren();一样的对象

    echo $c->name;

    $c->name="李四";  --------第一种修改成员变量值的方法

    echo $c->name;

    三、加载类   (重要)

    (1)建立要加载的文件 文件名为ren.class.php (不含其他代码时,可以只写开头不写结尾)

    (2)加载类的方法  7种  一两个时用前六个,多时用最后一个

    1、include("./ren.class.php");

    2、include "./ren.class.php";

    区别:include是将全部内容加载进来,一个出错,当前页面也崩溃,require只是加载相关部分

    3、require("./ren.class.php");

    4、require "./ren.class.php";

    区别:前者(3、4)考虑次数,会出现重复引入的问题;后者(5、6)只请求一次,请求了就不会再请求第二次   

    5、require_once("./ren.class.php");

    6、require_once "./ren.class.php";

    $r=new Ren();

    7、自动加载类  

    使用时的要求: 

    1、所有的类文件要写在同一个目录下

    2、类文件的命名规则要一致xx.class.php

    3、类的文件名要和类名保持一致(区分大小写)

           function __autoload($classname)

           {

                  require_once("./".$classname.".class.php");   //造对象的时候调用 

           }

    $r=new Ren();

  • 相关阅读:
    http数据返回值
    刷新 返回
    微信平台上遇到的bug
    iscroll修改
    iscroll
    a标签的herf和click事件
    ios9+xcode7 适配笔记
    xsd、wsdl生成C#类的命令行工具使用方法
    xcode更新,想想也是醉了
    关于#define预处理指令的一个问题
  • 原文地址:https://www.cnblogs.com/chenguanai/p/6738791.html
Copyright © 2011-2022 走看看