Home > Backend Development > PHP Tutorial > PHP advanced object construction and use of factory pattern_PHP tutorial

PHP advanced object construction and use of factory pattern_PHP tutorial

WBOY
Release: 2016-07-21 15:21:01
Original
882 people have browsed it

How to use the PHP design pattern factory pattern

Copy the code The code is as follows:

/*
* Practice the use of PHP design pattern factory pattern every day
* The PHP factory pattern is not difficult to understand. As the name suggests, it is a processing factory, and the factory manufactures products. As long as the product is manufactured
* There must be several elements: "method", "model", "factory floor".
*/
/*First example ordinary factory pattern
* */
abstract class model {//Product model
abstract function getNames();
}
class zhangsan extends model {//Product instance
function getNames(){
return "my name is zhengsan";
}
}
class lisi extends model{//Product instance
function getNames(){
return "my name is lisi";
}
}
abstract class gongchangModel {//Factory model
abstract function getZhangsan();
abstract function getLisi();
}
class gongchang extends gongchangModel{//Factory instance
function getZhangsan(){
return new zhangsan();
}
function getLisi(){
return new lisi();
}
}
$gongchang=new gongchang();//Instancing factory
$zhangsan=$gongchang->getZhangsan();//Manufacturing Product
echo $zhangsan->getNames();//Product output function
?>

I wrote an article about the factory design pattern before. In fact, the factory pattern It includes ordinary factory mode and abstract factory mode, but no matter what factory mode they are, they all have one function, and that is to generate objects.
Okay, let’s use the simplest example below to demonstrate the factory pattern in the PHP design pattern.
I have summarized the three elements of the factory model:
1. Product model
2. Product examples
3. Factory workshop
Copy Code The code is as follows:

abstract class prModel {//Product model
abstract function link();
}
class webLink extends prModel{//Example a product
public function link(){
echo "www.jb51.net";
}
}
class gongchang {//Factory
static public function createLink (){
return new webLink();
}
}
$weblink=gongchang::createLink();//Create an object through the factory
$weblink ->link();//Output www.jb51.net
?>

The above method simply explains how to use the factory class. Focus on object-oriented

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325000.htmlTechArticleHow to use PHP design pattern factory pattern Copy the code as follows: ?php /* * Daily practice PHP design pattern How to use factory mode* PHP factory mode is not difficult to understand. As the name suggests,...
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