redis - php两种封装类的优缺点?
php很多内置的类或扩展类比较粗糙,需要人为的封装一遍,于是有了两种封装方式,
1直接继承
2内部实例化
如redis类,
可以
<code>namespace lib class redis extends \Redis {} </code>
也可以
<code>namespace lib class redis { private $_redis = null; public function __construct() { $this->_redis = new \Redis() } } </code>
第一种方法的优点是方便,无须把所有的方法重写一遍,但是不太好统一捕捉异常,
第二种就是麻烦些,需要重写一遍所有的方法,(虽然可以用魔术方法),捕捉异常方便些。
大家用的哪种?为什么呢?
回复内容:
php很多内置的类或扩展类比较粗糙,需要人为的封装一遍,于是有了两种封装方式,
1直接继承
2内部实例化
如redis类,
可以
<code>namespace lib class redis extends \Redis {} </code>
也可以
<code>namespace lib class redis { private $_redis = null; public function __construct() { $this->_redis = new \Redis() } } </code>
第一种方法的优点是方便,无须把所有的方法重写一遍,但是不太好统一捕捉异常,
第二种就是麻烦些,需要重写一遍所有的方法,(虽然可以用魔术方法),捕捉异常方便些。
大家用的哪种?为什么呢?
继承
继承的特点有
- ✔实现成本低
- ✔调用者可以沿用原有的接口使用,学习成本低
- ✔原有的功能无需任何代码如常工作
-
✘无法隐藏或改变原有的功能
- 其实你可以这么做,就好像你确实可以用铁丝去捅插座眼一样
- 改变输入、改变输出、改变行为都属于改变功能,比如原来抛的异常现在不抛了,原来return false现在变异常了,原来输入的是青椒现在变牛肉了等等
✘正交性弱,没有做到屏蔽父类的依赖,需要更换父类的时候显得脆弱
常见的合适的应用有
- 增加日志记录等不影响原有逻辑的“旁路逻辑”
-
增加一些方法,比如原有的某一些方法a()b()c()总是连续一起调用,增加一个doABC()方法
- 仅限简单的少量的方法,如果要加复杂的功能或者大量的方法,还是建议用组合
增加一些静态/工厂方法,比如
new Redis(ip, port)
=>OurRedis::getInstance()
一句话说就是原有行为不变,is-a的场景用继承。
组合
组合的特点有
- ✘实现成本略高,创建对象的过程可能会变复杂
- ✘调用者需要理解新的接口
- ✘需要转发才能让原有的功能工作
- ✔很容易隐藏/屏蔽原有的部分功能
- ✔正交性强,可以通过更换内部的对象适配不同的情况而保持外部接口不变
常见的合适的应用有
- 一切涉及改变行为的需求
- 未来依赖会变的场景,比如redis会变成memcache,或者mysql会变mariadb之类
- 需要屏蔽细节的场景,比如Session Cache等需求确实用到redis,但对外肯定要屏蔽“这是个redis实现的session”。否则恐龙会出现在你背后把你脑袋咬掉
一句话就是某个功能依赖另一个功能,use-a或has-a的场景用组合
偏题,predis用起来还不错,可以一试
如果你看过任何一本设计模式的书,肯定都是推荐你使用第二种。
它们奉行的原则是:组合优于继承。
不过,还得看自己需求。如果系统的接口和你的类提供的一致,可以试试第一种。如果不一致,选择第二种。
如果你正在为系统设计 DB 中间层,需要适配不同的数据库,选择第二种。
楼主你干脆用Java吧,别用PHP了。用好一门语言就要掌握这门语言的特性和风格,按照这门语言的风格去行事。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
