首页
统计
留言
Search
1
PHP中使用反射
995 阅读
2
phpstorm配置SFTP
940 阅读
3
Go语言——结构体
792 阅读
4
PhpStorm 使用 AI 代码生成工具 Codeium
779 阅读
5
关于PHP的垃圾回收机制
763 阅读
后端
PHP
Go
数据库
其他
前端
其他技术
生活杂谈
登录
Search
标签搜索
Laravel
Mysql
RPC
Redis
Liunx
PHP
CSS
ES
算法
开发工具
断点续传
反射
phpstorm
工具
防盗链
CURL
设计模式
面试
Nginx
搜索引擎
quhe.net
首页
栏目
后端
PHP
Go
数据库
其他
前端
其他技术
生活杂谈
页面
统计
留言
搜索到
24
篇与
PHP
的结果
2022-08-04
使用字符串作为laravel模型的主键时需注意
当使用非自增int类型字段作为model的主键时,需要手动在模型里设置属性:主键字段名主键类型是否自增代码示例namespace App\Models; use Dcat\Admin\Traits\HasDateTimeFormatter; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class ProductAttr extends Model { use HasDateTimeFormatter; use SoftDeletes; //主键类型 protected $keyType = 'string'; //自定义主键字段 protected $primaryKey = 'code'; //是否自增 public $incrementing = false; //表名 protected $table = 'product_attrs';
2022年08月04日
37 阅读
0 评论
0 点赞
2022-06-21
谈一谈PHP编程中的SOLID原则
什么是SOLID原则软件设计有很多原则,比如软件设计上的 SOLID principle,单元测试中的 FIRST和AAA,代码实现上的 DRY principle 等。熟悉这些原则,可以把我们的经验上升到理论高度,有利于程序员的成长,也便于团队带头人和组员控制软件质量。其中面向对象设计的 SOLID 原则是创建易于维护、扩展和测试的软件的基本指南。 SOLID 是下面几个英文词组的缩写1.Single-Responsibility principle (单一职责原则)2.Open-Close principle (开放扩展,封闭修改)3.Liskov substitution principle (Liskov 替换原则)4.Interface segregation principle (接口隔离原则)5.Dependency inversion principle (依赖反转原则)单一职责原则 (SRP)单一职责原则 (SRP) 指出一个类应该只有一个职责。 这意味着一个类应该只有一个改变的理由。 让我们看一下如何在 PHP 编程中应用 SRP 的示例。// Example of a class with multiple responsibilities class User { public function login($username, $password) { // Authenticate user } public function updateProfile($userId, $data) { // Update user profile } public function sendEmail($userId, $subject, $body) { // Send email to user } } // Refactored class with single responsibility class Authenticator { public function login($username, $password) { // Authenticate user } } class ProfileUpdater { public function update($userId, $data) { // Update user profile } } class EmailSender { public function send($userId, $subject, $body) { // Send email to user } }在上面的示例中,我们从一个名为“User”的类开始,该类具有多项职责:身份验证、配置文件更新和电子邮件发送。 然后我们将该类重构为三个独立的类,每个类都有一个职责:身份验证、配置文件更新和电子邮件发送。开闭原则(OCP)开闭原则 (OCP) 指出软件实体应该对扩展开放,对修改关闭。 这意味着您应该能够在不修改其源代码的情况下扩展类的行为。 让我们看一下如何在 PHP 编程中应用 OCP 的示例。// Example of a class without OCP class Car { public function drive() { // Drive the car } } // Extended class with OCP class CarWithNavigation extends Car { public function navigate() { // Navigate using GPS } }在上面的示例中,我们从一个名为“Car”的类开始,该类只有一个驾驶汽车的方法。 然后我们扩展该类以创建一个名为“CarWithNavigation”的新类,该类具有使用 GPS 进行导航的附加方法。 通过扩展原始类,我们能够在不修改原始类源代码的情况下添加新功能。里氏替换原则 (LSP)Liskov 替换原则 (LSP) 指出超类的对象应该能够被其子类的对象替换而不影响程序的正确性。 让我们看一下如何在 PHP 编程中应用 LSP 的示例。// Example of a class that violates LSP class Rectangle { public function setWidth($width) { $this->width = $width; } public function setHeight($height) { $this->height = $height; } public function getArea() { return $this->width * $this->height; } } class Square extends Rectangle { public function setWidth($width) { $this->width = $width; $this->height = $width; } public function setHeight($height) { $this->height = $height;` # 接口隔离原则(ISP) “I”代表面向对象设计SOLID原则中的Interface Segregation Principle(ISP)。 ISP 声明不应强迫客户端依赖于它们不使用的接口。 换句话说,拥有许多小的、特定的接口比拥有几个大的、通用的接口要好。 让我们看一个如何在 PHP 编程中应用 ISP 的示例。 `// Example of a class that violates ISP interface Document { public function open(); public function save(); public function close(); } class TextDocument implements Document { public function open() { // Open text document } public function save() { // Save text document } public function close() { // Close text document } } class PdfDocument implements Document { public function open() { // Open PDF document } public function save() { // Save PDF document } public function close() { // Close PDF document } }在上面的示例中,我们从一个名为“Document”的接口开始,该接口具有三个方法:打开、保存和关闭。 然后我们创建了两个实现 Document 接口的类:TextDocument 和 PdfDocument。 但是,TextDocument 类不需要 close 方法,因为它只处理文本文档。 同样,PdfDocument 类不需要保存方法,因为 PDF 文档通常是只读的。 通过将 Document 接口分解为更小、更具体的接口,我们可以避免强制客户端依赖于它们不使用的方法。要在 PHP 编程中应用 ISP,您的目标应该是创建特定于客户需求的接口。 这将使您的代码更加模块化,更易于维护,并且更不容易出错。依赖反转原则(DIP)D”代表面向对象设计SOLID原则中的Dependency Inversion Principle(DIP)。 DIP 指出高级模块不应该依赖于低级模块,但两者都应该依赖于抽象。 另外,抽象不应该依赖于细节,而细节应该依赖于抽象。让我们看一下如何在 PHP 编程中应用 DIP 的示例。// Example of a class that applies DIP interface PaymentGateway { public function charge($amount); } class PaymentProcessor { private $gateway; public function __construct(PaymentGateway $gateway) { $this->gateway = $gateway; } public function processPayment($amount) { $this->gateway->charge($amount); } } class PaypalGateway implements PaymentGateway { public function charge($amount) { // Charge payment using Paypal API } }在更新的示例中,我们引入了 PaypalGateway 类实现的 PaymentGateway 接口。 PaymentProcessor 类现在依赖于 PaymentGateway 接口而不是 PaypalGateway 类,使其更灵活,将来更容易更改。 我们现在可以轻松地将 PaypalGateway 类换成另一个实现 PaymentGateway 接口的支付网关。要在 PHP 编程中应用 DIP,您的目标应该是创建将高级模块与低级模块隔离开来的抽象。 这将使您的代码更加模块化,更易于维护,并且更不容易出错。
2022年06月21日
23 阅读
0 评论
0 点赞
2022-05-25
Laravel分页器如何在不修改paginate结构的情况下优雅的修改items数据
先把paginate对象赋值给一个变量$users =User::paginate(); 然后调用集合的遍历方法修改数据。因为集合里的元素都是对象,所以这里会引用传递。修改到原来的值:$users->getCollection()->transform(function ($item){ $item->age = 18; return $item; });容易犯错的地方//如果是这样直接调用会破坏分页器的数据结构,因为这个方法返回的是一个collect集合 $users = User::paginate()->map(function(){ })
2022年05月25日
147 阅读
0 评论
3 点赞
2022-02-08
Laravel 修改模型默认主键的坑
Bug: Laravel 字符主键获取为0laravel的orm中默认的主键$primaryKey为id,主键类型$keyType 是int类型, 所以如果我们修改了默认主键当model的主键为 string 类型的时候,会获取到0的值。解决方法: 在model 里面添加protected $keyType = 'string';
2022年02月08日
35 阅读
0 评论
0 点赞
2022-01-15
PHP 8.1 新特性:纤程 Fiber
介绍纤程(Fiber)代表了有完整栈、可中断的功能。Fiber 可以从调用堆栈中的任何位置挂起,在 fiber 内暂停执行,直到稍后恢复 fiber。类摘要final class Fiber { /* 方法 */ public __construct(callable $callback) public start(mixed ...$args): mixed public resume(mixed $value = null): mixed public throw(Throwable $exception): mixed public getReturn(): mixed public isStarted(): bool public isSuspended(): bool public isRunning(): bool public isTerminated(): bool public static suspend(mixed $value = null): mixed public static getCurrent(): ?Fiber }Fiber::__construct — 创建一个纤程实例Fiber::start — 开始执行纤程Fiber::resume — 使用值恢复一个纤程Fiber::throw — 抛出异常恢复纤程Fiber::getReturn — 获取纤程返回的值Fiber::isStarted — 判断纤程是否启动Fiber::isSuspended — 判断纤程是否暂停Fiber::isRunning — 判断纤程是否在运行Fiber::isTerminated — 确认纤程是否已中止Fiber::suspend — 暂停当前纤程Fiber::getCurrent —获取当前执行的纤程实例纤程概要(PHP 8 >= 8.1.0)纤程(Fiber)表示一组有完整栈、可中断的功能。 纤程可以在调用堆栈中的任何位置被挂起,在纤程内暂停执行,直到稍后恢复。纤程可以暂停整个执行栈,因此该函数的直接调用者不需要改变该函数的执行方式。可以在任何地方使用Fiber::suspend()调用栈中断执行(也就是说,Fiber::suspend()的调用可以在一个深度嵌套的函数中调用,即使它不存在)。不像没有栈的生成器,每个Fiber有它自己的调用栈,允许它们在深度嵌套函数调用中暂停。一个函数声明一个中断点(即, 调用Fiber::suspends())不需要改变他的返回类型,而不像使用 yield 的函数,必须返回Generator 实例。纤程可以在任何函数调用中暂停,包括那些在 PHP VM 中被调用的函数,比如用于array\_map()的函数或者提供 Iterator 实例以被 foreach 调用的方法。纤程一旦被暂停,可以使用 Fiber::resume() 传递任意值、或者使用 Fiber::throw() 向纤程抛出一个异常以恢复运行。这个值或者异常将会在 Fiber::suspend() 中被返回(抛出)。示例:<?php $fiber = new Fiber(function (): void { $value = Fiber::suspend('fiber'); echo "Value used to resume fiber: ", $value, PHP_EOL; }); $value = $fiber->start(); echo "Value from fiber suspending: ", $value, PHP_EOL; $fiber->resume('test'); ?>输出:Value from fiber suspending: fiber Value used to resume fiber: test
2022年01月15日
51 阅读
0 评论
0 点赞
1
2
3
...
5