mehedimi/new-instance

一个包装,用于链式调用类方法而不存储变量

1.0 2021-07-22 06:26 UTC

This package is auto-updated.

Last update: 2024-09-22 13:23:52 UTC


README

创建类的实例并链式调用方法,而不存储在变量中。

安装

要通过composer安装此包,只需在您的终端运行以下命令。

composer require mehedimi/new-instance

使用

只需在您的任何PHP类中使用Mehedi\NewInstance\NewInstance特性

<?php
use Mehedi\NewInstance\NewInstance;

class Post
{
    use NewInstance;
    
    public function find()
    {
        //
    }
}

// Then 
Post::newInstance()->find();

或者,您还可以创建任何PHP类的单例实例。

<?php
$post1 = Post::newSingleInstance();
$post2 = Post::newSingleInstance();

// Here $post1 and $post2 object are same

此外,您还可以在类构造函数中接受一个或多个参数

use Mehedi\NewInstance\NewInstance;

class Post
{
    use NewInstance;
    
    public function __construct($hello, $word)
    {
        //
    }
}
Post::newSingleInstance('hello', 'word');
// OR
Post::newInstance('hello', 'word');