zoukankan      html  css  js  c++  java
  • tostring方法,克隆对象和加载类

    一、__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、类的文件名要和类名保持一致(区分大小写)

  • 相关阅读:
    TCP/IP断网仍然显示连通状态禁止“媒体感知”
    ASP.NET页面优化,提高载入速度方法大全
    国家气象局提供
    datatable&list<T>
    windows mobile C# net3.5 如何捕捉Arrow的CheckBox的KeyDown事件?
    hhclass 类 RAPI OpenNETCF.Desktop.Communication.DLL相关下载
    string和stringBuilder区别
    windows mobile全屏设置笔记
    CSS浮动(float,clear)通俗讲解
    js图片延时加载(jquery特效)
  • 原文地址:https://www.cnblogs.com/zqld/p/6739219.html
Copyright © 2011-2022 走看看