search
Homephp教程php手册PHP高级对象构建 多个构造函数的使用

PHP高级对象构建 多个构造函数的使用

Jun 13, 2016 pm 12:02 PM
phpone timeundercodeusefunctionMultipleobjectConstructDemoofExampleadvanced

下面就用一段代码示例来演示一下PHP高级对象构建中的使用多个构造函数进行对象构建的原理。

复制代码 代码如下:


class classUtil {//这是一个参数处理的类
public static function typeof($var){
if (is_object($var)) return get_class($var);//如果是对象,获取类名
if (is_array($var)) return "array";//如果是数组,返回"array"
if (is_numeric($var)) return "numeric";//如果是数字,返回"numeric"
return "string";//字符串返回 "string"
}
public static function typelist($args){
return array_map(array("self","typeof"),$args);//数组循环通过调用self::typeof处理$args中的每个元素
}
public static function callMethodForArgs($object,$args,$name="construct"){
$method=$name."_".implode("_",self::typelist($args));//implode 是把数组元素用"_"连接成一个字符串
if (!is_callable(array($object,$method))){//is_callable()函数测试$object::$method是不是可调用的结构
echo sprintf("Class %s has no methd '$name' that takes".
"arguments (%s)",get_class($object),implode(",",self::typelist($args)));
call_user_func_array(array($object,$method),$args);//call_user_func_array函数调用$object::$method($args)
}
}
}
class dateAndTime {
private $timetamp;
public function __construct(){//自身的构造函数
$args=func_get_args();//获取参数
classUtil::callMethodForArgs($this,$args);//调用参数处理类的方法
}
public function construct_(){//参数为空的时候
$this->timetamp=time();
}
public function construct_dateAndTime($datetime){//为类自身的时候
$this->timetamp=$datetime->getTimetamp();
}
public function construct_number($timestamp){//为数字的时候
$this->timetamp=$timestamp;
}
public function construct_string($string){//为时间型字符串时候
$this->timetamp=strtotime($string);
}
public function getTimetamp(){//获取时间戳的方法
return $this->timetamp;
}
}
?>


以上方法,就说明了多个构造函数的使用方法,其实,很简单,主要是对参数进行了处理,不管是参数是字符,还是数字,还是类,都先进了不同的处理,这样就加大了代码的灵活性。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)