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

           function __autoload($classname)

           {

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

           }

    $r=new Ren();

  • 相关阅读:
    Flutter 容器(4)
    Linux 安装maven环境
    ajaxStart、ajaxStop使用注意事项
    jquery里用正则来验证密码,必须包含大小写字母,数字及特殊符号,或最少包含两种
    Nginx 相关介绍(Nginx是什么?能干嘛?配有图片示例)
    常见web攻击总结
    用php的chr和ord函数实现字符串和ASCII码互转
    消息队列设计精要
    mysql索引总结----mysql 索引类型以及创建
    MySQL事务隔离级别详解
  • 原文地址:https://www.cnblogs.com/mengshenshenchu/p/6740351.html
Copyright © 2011-2022 走看看