PHP advanced object construction and the use of multiple constructors_PHP tutorial

WBOY
Release: 2016-07-21 15:21:03
Original
956 people have browsed it

The following is a code example to demonstrate the principle of using multiple constructors for object construction in PHP advanced object construction.

Copy code The code is as follows:

class classUtil {//This is a parameter processing Class
public static function typeof($var){
if (is_object($var)) return get_class($var);//If it is an object, get the class name
if (is_array($var) ) return "array";//If it is an array, return "array"
if (is_numeric($var)) return "numeric";//If it is a number, return "numeric"
return "string"; //String return "string"
}
public static function typelist($args){
return array_map(array("self","typeof"),$args);//Array loop through Call self::typeof to process each element in $args
}
public static function callMethodForArgs($object,$args,$name="construct"){
$method=$name."_ ".implode("_",self::typelist($args));//implode is to use "_" to connect the array elements into a string
if (!is_callable(array($object,$method) )){//is_callable() function tests whether $object::$method is a callable structure
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 function call $object::$method($args)
}
}
}
class dateAndTime {
private $timetamp;
public function __construct(){//own constructor
$args=func_get_args();//Get parameters
classUtil::callMethodForArgs($this,$args);//Call the method of parameter processing class
}
public function construct_(){ //When the parameter is empty
$this->timetamp=time();
}
public function construct_dateAndTime($datetime){//When it is the class itself
$this-> ;timetamp=$datetime->getTimetamp();
}
public function construct_number($timestamp){//When it is a number
$this->timetamp=$timestamp;
}
public function construct_string($string){//When it is a time string
$this->timetamp=strtotime($string);
}
public function getTimetamp(){// Method to obtain timestamp
return $this->timetamp;
}
}
?>

The above method illustrates the use of multiple constructors The method of use is actually very simple. It mainly processes the parameters. Whether the parameters are characters, numbers, or classes, different processing is advanced, which increases the flexibility of the code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324997.htmlTechArticleThe following is a code example to demonstrate the use of multiple constructors for object construction in PHP advanced object construction. principle. Copy the code The code is as follows: ?php class classUtil {//This is...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!