*@return $this
*\/The
Public function set (string $key, $value)
The{
If (in_array ($key, $this->properties)){
$this-> attribute [$key]=$value;
}The
Returns $this.
}The
}The
For example, introducing type checking may become more complex. The
Repository classes or storage patterns that typically implement a set of items. For this reason, it is useful to have a basic class with all of the following methods: Get () – import items from the collection () – add items to the collection has () – allow items to be added to the collection () – ensure that items can be added to the collection. This list and my method of designing the repository are greatly influenced by laravel’s collection class and symfny’s parameterbag class. The following is a repository example, which is an abstract class that implements all of these methods. The
‹ repository){
$this->repository= new class extension repository {};
}The
Return $this->repository.
}The
}Note that the method in this repository is used when calling. For example, the get () method uses the has () method to protect the unset index of a property, rather than reusing the same isset () check. This is important because the has () logic in the extension class may change. The
Up to now, objects have been created outside the repository and pushed to the repository. I like to integrate the object factory into the repository so that it can work as a lazy loader. The following is an example of this type of class: Male attributes[$name]);
}The
\/**The
*Add values to the repository
*The
*@Param string $name
*@Param$value
*The
*@return $this
*\/The
Public function set (string $name, $value)
The{
If ($i-> allow ($name){
$this-> attribute [$name]=$value;
}The
Returns $this.
}The
\/**The
*Verify that is a value type that can be saved
*The
*@Param string | int$name
*The
*@Carriage return Boolean
*\/The
Allow public function ($name): bool
The{
Return in_array ($name, $this->getallowedproperties());
}The
\/**The
*Available \/ allowed values
*The
*@Param string $name
*@Param mix | null$default
*The
*@Return mix
*\/The
Public function get (string $name, $default=null)
The{
If ($this->has ($name)){
Return $this->attribute[$name];
}Elseif ($this-> allow ($name){
$this->set ($name, $this->factory ($name));
Return $this->attribute[$name];
}The
Return $default;
}The
\/**The
*Get allowed properties
*The
*@Return array
*\/The
Public function getallowedproperties(): array
The{
Return $this->properties.
}The
}This adds an abstract method called \
The following is an implementation example that returns objects in the wp_post class. ($name]);
$posts[0] return;
}The
}Now you can use the get () method to use the post slug or post ID. if the post has not been queried, it will be placed in the collection and returned. Twice
If requested again, the object already exists. The
This mode can improve the efficiency of PHP and developers. This is especially true when placing repositories in the object cache. In WordPress container creation, one of the common anti patterns is the \
The following is an example of a plug-in that usually requires the available \
$this->apiclient= new apiclient;
}The
Return $this->apiclient.
}The
Public function getsettings(): setting
The{
If (! $this-> setting){
$this->settings= new settings;
}The
Return to $this->settings.
}The
}Now we have a single ton that provides access to the two different classes we need, but each is loaded late. Available: Male getapiclient(); If you need to add a property to add a class to this container, and there are too many methods, it is difficult to explain. I recently used this pattern and found that I often switch to using any repository, as shown above inside the container. In the breakup scenario base mode, one thing to consider is to make the user’s life easier and help code expansion. Don’t work on these details. GitHub also provides packages that use these repository patterns.