Làm cách nào để bạn gọi một phương thức tĩnh trong Laravel?

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
3

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
5

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
7

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
8

Một hạn chế của ngôn ngữ PHP là không có khả năng tạo nhiều hàm tạo. Ví dụ, trong Java, bạn có thể tạo các hàm tạo khác nhau miễn là chúng có chữ ký phương thức duy nhất. Điều này cho phép lập trình viên tạo ra các cách riêng biệt để tạo cùng một đối tượng tùy thuộc vào tình huống.

Vì ngôn ngữ PHP thiếu tính năng này nên chúng tôi giải quyết vấn đề này bằng cách sử dụng các mẫu thiết kế phần mềm khác nhau. Hầu hết thời gian, chúng tôi chỉ tạo một nhà máy. Sau đó, nhà máy xử lý các trường hợp sử dụng khác nhau này để tạo đối tượng

Một loại mẫu nhà máy ít được biết đến hơn là mẫu phương thức nhà máy tĩnh. Mẫu này cho phép chúng tôi giải quyết vấn đề nhiều hàm tạo. Nhưng nó cho phép chúng tôi làm như vậy mà không cần tạo một lớp nhà máy mà chúng tôi có thể không cần

Bây giờ, tôi đã viết về cách sử dụng mẫu phương thức tĩnh của nhà máy với WordPress trước đây. Tuy nhiên, vì tôi đã sử dụng Laravel nhiều hơn với các dự án khách hàng và xây dựng Ymir, tôi cảm thấy cũng đáng để xem lại nó trong bối cảnh đó. Cụ thể, tôi nghĩ bạn nên xem cách bạn có thể sử dụng nó với các mô hình Eloquent

Các phương thức nhà máy tĩnh đã có trong Eloquent

Eloquent có một số cách khởi tạo đối tượng mô hình mới. Nó có một hàm tạo mà bạn có thể sử dụng. Nhưng cũng có các phương thức

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
9 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
0 ánh xạ tới lớp Eloquent
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
0. [Ngoài ra còn có nhiều phương thức cụ thể cho trường hợp sử dụng hơn như
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
1 hoặc
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
2. ]

Bản thân các phương thức này là các phương thức nhà máy tĩnh. Vấn đề là chúng rất chung chung. Tất cả đều lấy

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
3 làm tham số

Đó là bởi vì các phương thức này sẽ gán hàng loạt các thuộc tính này cho mô hình khi bạn tạo nó. [Chà, đó là trừ khi mô hình có các thuộc tính được bảo vệ. ] Nếu không muốn gán hàng loạt, bạn cần gán từng thuộc tính một

Một điểm khởi đầu tuyệt vời

Điều tuyệt vời khi các phương thức này là chung chung là chúng ta có thể sử dụng chúng làm điểm khởi đầu cho các phương thức của riêng mình. Những gì chúng tôi sẽ làm là tạo các phương thức của riêng mình, sau đó sẽ gọi các phương thức tĩnh

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4 hoặc
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
5. Hãy xem một ví dụ

namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}

Trên đây là một mô hình được gọi là

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6. Lớp này có một phương thức nhà máy tĩnh gọi là
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
7. Phương thức có hai tham số.
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
8 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
9 mà chúng tôi gõ gợi ý

Khả năng có gợi ý loại là một lý do khác tại sao việc tạo các phương thức nhà máy tĩnh tùy chỉnh có thể có lợi. Nó cho phép bạn tăng loại an toàn. Đây là điều mà tôi luôn coi trọng khi viết code

Phần còn lại của phương thức nhà máy tĩnh chỉ gọi phương thức chung chung

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
9. Chúng tôi truyền cho nó một mảng với các thuộc tính mà chúng tôi muốn nó gán cho đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6 mới. Mảng chứa ID của người dùng là tác giả của bài đăng và tiêu đề

Còn phương thức make thì sao?

Đối với nhiều nhà phát triển Laravel, phương thức

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
7 là đủ. Nhưng tôi có xu hướng thích sử dụng phương pháp
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
5 hơn và trì hoãn việc lưu mô hình cho đến khi tôi hoàn tất việc sửa đổi mô hình. Vì vậy, những gì tôi cần là một phương pháp nhà máy tĩnh
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44

Có cả phương thức

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
7 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44 thực sự khá dễ thực hiện. Chúng ta có thể làm điều tương tự mà Laravel đã làm với
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
5. Nếu bạn nhìn vào các phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
5 trong lớp
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
0, chúng trông như thế này

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}

Về cơ bản, sự khác biệt duy nhất giữa cả hai phương pháp là việc sử dụng hàm trợ giúp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
32 để lưu mô hình mà chúng ta đã tạo. Đó là nó. Vì vậy, chúng tôi chỉ có thể làm điều tương tự khi xây dựng các phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
7 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44 của chúng tôi

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4

Thông thường, phương thức

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
35 trả về giá trị boolean. Vì vậy, chúng tôi phải chia nhỏ mọi thứ thành nhiều dòng mã. Bằng cách sử dụng hàm trợ giúp
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
32, chúng ta có thể sử dụng phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
35, nhưng vẫn trả về đối tượng bài đăng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6 mới

Một vi dụ khac. Yêu cầu biểu mẫu

Các phương pháp nhà máy tĩnh rất tuyệt vời với các mô hình Eloquent khác. Nhưng chúng cũng phối hợp tốt với các tính năng khác của Laravel. Một trong số đó là yêu cầu biểu mẫu

Yêu cầu biểu mẫu là một tính năng thú vị cho phép bạn tạo các lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
39 tùy chỉnh. Các lớp
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
39 tùy chỉnh này thuận tiện cho việc lưu trữ tất cả mã xác thực của bạn. Nhưng bạn cũng có thể sử dụng chúng để cung cấp các phương thức trợ giúp để tương tác với yêu cầu HTTP

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
3

Vì vậy, ở trên là lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
41 mà chúng ta có thể sử dụng để tạo các đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6 của mình. Nó có ba phương pháp.
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
43,
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
45. Phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
43 xác định xem người dùng hiện tại có thể xử lý yêu cầu hay không. Chúng tôi chỉ trả về
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
47 có nghĩa là bất kỳ người dùng nào cũng có thể xử lý yêu cầu này

Trong khi đó, phương thức

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44 chỉ trả về giá trị
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
9 từ yêu cầu. Đây là lý do tại sao phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
45, trả về các quy tắc xác thực để sử dụng với yêu cầu, có các quy tắc xác thực cho
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
9. Chúng tôi cần đảm bảo rằng nó luôn hiện diện và đó là một chuỗi

Tạo phương thức nhà máy tĩnh cho yêu cầu biểu mẫu

Bây giờ, chúng ta chỉ cần cập nhật lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6 của mình. Chúng tôi sẽ thêm một phương thức xuất xưởng tĩnh lấy đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
41 làm đối số. Bạn có thể thấy lớp sửa đổi bên dưới

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4

Chúng tôi đã thêm hai phương thức nhà máy tĩnh mới.

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
84 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
85. Hai phương thức này hoạt động giống như hai phương thức nhà máy tĩnh trước đây của chúng tôi. Phương thức tạo gọi phương thức tạo và lưu mô hình bằng hàm
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
32

Điều thú vị là những gì chúng tôi làm trong phương pháp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
84. Chúng tôi sử dụng các giá trị được trả về từ các phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
41 và chuyển chúng sang phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
44. Điều này cho phép chúng tôi tận dụng mã của mình từ phương pháp nhà máy tĩnh khác trong khi cung cấp một cách khác để tạo đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6

Khi nào bạn nên sử dụng một nhà máy truyền thống?

Như bạn có thể thấy, các phương thức tĩnh của nhà máy thường là quá đủ cho nhu cầu tạo mô hình của bạn. Điều đó nói rằng, có những trường hợp tôi phải sử dụng một lớp nhà máy truyền thống kết hợp với các phương thức nhà máy tĩnh. Một điều phổ biến là nếu mô hình của bạn có mối quan hệ một-nhiều và bạn cần tạo cả mô hình cha và mô hình con cùng một lúc

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
8

Để chứng minh điều này, hãy lấy mô hình

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61 được hiển thị ở trên. Nó có mối quan hệ một-nhiều với mô hình
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62. Khi tạo một đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61, bạn sẽ muốn thêm ít nhất một
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62 cho người dùng quản trị

Bây giờ, bạn có thể tạo một phương thức xuất xưởng tĩnh trong lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61 cũng tạo đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62. Nhưng tôi cảm thấy như nó đánh bại mục đích của phương thức nhà máy tĩnh là trở thành một hàm tạo tùy chỉnh. Đây thực sự là một tình huống mà bạn muốn có một lớp nhà máy như thế này

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
6

Vì vậy, lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
67 ở trên có một phương thức duy nhất gọi là
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4. Bạn vượt qua nó ba đối số.
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
69,
namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}
60 và
namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}
61. Sau đó, chúng tôi sử dụng ba đối số này để tạo các đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61 và
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62 trong cơ sở dữ liệu

Phương thức bắt đầu bằng cách tạo đối tượng

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61. Sau đó, chúng tôi gọi phương thức
namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}
65 trả về đối tượng mối quan hệ
namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}
66. Sau đó, chúng tôi gọi phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4 của nó sẽ tạo
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62 và liên kết nó với đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61

Chúng tôi kết thúc bằng cách trả lại đối tượng

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61 đã tạo. Và như thế, phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
4 của chúng ta đã tạo ra hai đối tượng trong cơ sở dữ liệu cùng một lúc. Điều này sẽ ít có ý nghĩa hơn khi là một phương thức xuất xưởng tĩnh trong lớp
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61

Kết hợp với các phương thức nhà máy tĩnh

Điều đó nói rằng, điều này không có nghĩa là bạn không thể sử dụng các phương thức tĩnh của nhà máy với một lớp nhà máy. Cả hai mẫu xuất xưởng đều phục vụ các mục đích khác nhau, vì vậy không có gì sai khi sử dụng cả hai cùng một lúc

namespace App;
 
use Illuminate\Database\Eloquent\Model;

class BlogPost extends Model
{
    /**
     * Make a new BlogPost object from the given User object and save it to the database.
     */
    public static function createFromUser[User $author, string $title]: self
    {
        return self::create[[
            'user_id' => $author->id,
            'title' => $title,
        ]];
    }

    // ...
}
6

Đây là lớp

namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
67 được cập nhật để sử dụng các phương thức tĩnh của nhà máy. Chúng ta tạo đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
61 bằng phương thức
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
95. Sau đó, chúng tôi chuyển nó sang phương thức nhà máy tĩnh
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
96 để tạo đối tượng
namespace Illuminate\Database\Eloquent;

class Builder
{
    // ...

    public function create[array $attributes = []]
    {
        return tap[$this->newModelInstance[$attributes], function [$instance] {
            $instance->save[];
        }];
    }

    // ...

    public function make[array $attributes = []]
    {
        return $this->newModelInstance[$attributes];
    }

    // ...
}
62

Mang lại thêm một chút cấu trúc cho các mô hình Eloquent của bạn

Vì vậy, điều đó kết thúc cái nhìn của chúng ta về các phương thức nhà máy tĩnh. Giống như tất cả các mẫu phần mềm, bạn không muốn sử dụng chúng cho mọi vấn đề bạn gặp phải. Tuy nhiên, với Eloquent, chúng là một công cụ vô giá để mang lại một số cấu trúc để tạo các đối tượng mô hình

Làm cách nào để gọi phương thức tĩnh PHP?

Để gọi một phương thức tĩnh từ một lớp con, hãy sử dụng từ khóa cha bên trong lớp con . Ở đây, phương thức tĩnh có thể là công khai hoặc được bảo vệ.

Khi nào sử dụng các phương thức tĩnh trong laravel?

Do đó, bất kỳ logic nào có thể được chia sẻ giữa nhiều phiên bản của một lớp nên được trích xuất và đặt bên trong phương thức tĩnh. Các phương thức tĩnh PHP thường được sử dụng trong các lớp chỉ chứa các phương thức tĩnh thường được gọi là các lớp tiện ích của các khung công tác PHP như Laravel và CakePHP.

Bạn có thể gọi phương thức tĩnh từ ví dụ không?

Gọi một phương thức tĩnh . Cú pháp Java cho phép gọi các phương thức tĩnh từ một thể hiện .

Chủ Đề