Render the shortcut code with the corresponding post collection
*The
*@Param string $collection\u name
*@Param string $template
*The
*@Return string
*\/The
Protected function generichandler (string $collection\u name, string $template)
The{
$posts= system: getinstance() – >getcontainer() – >get ($collection\u name) ->getposts();
Ob_start();
Include $template;
Return to ob\u get\u clean();
}The
\/**The
*Convert shortcut code names to CamelCased parameters
*The
*@param $string
*The
*@Return string
*\/The
Protected function to camelCase ($string): String
The{
$string = str_replace (‘-‘,’, $string);
$string = str\u replace (‘\’,’, $string);
$string = ucwords (strtower ($string));
$string = str_replace (‘,’ ‘, $string);
Return $string;
}The
}This will be set to: male \u dir\u. \
\
];
New shortcut codes ($shortcodes); As you can see, you can add a row of tags and template paths to the array to add other shortcut code. These are 3 shortcuts, 100 shortcuts and the same. In fact, as long as the shortcuts are used, the post class will be instantiated and the result query will be executed. These objects can be reused throughout the application. To illustrate this and show the same system used to inject dependencies, let’s create another implementation of the same container. In this example, in some cases, you will bypass the WordPress theme system and return to one of the views instead of a specific URL. Male query_vars[\
Switch ($wp->query\u vars[\
Case \
$route= \
broken
Cabinet \
$route= \
broken
Default:
$path =null;
broken
}The
If ($path){
Status header (200);
$controller= new controller ();
Echo$controller->$route (system:: getinstance() ->getcontainer() ->get ($route);
Export
}The
}The
});
});
Class controller{
Public function recent articles ($posts Posts posts)
The{
$posts = $posts->getposts();
}The
Public function prodcuts (post $posts)
The{
$posts = $posts->getposts();
}The
}In this example, I connect to the initial hook parse_request to verify whether one of the paths matches. If so, I call the class named controller to generate the view. This class is close to the stub and is used to display dependency injection, but can be used to create views. If you do this, you may use fastroute for more complex operations. However, a simple router illustrates this concept. In addition, although this is not a bad start, the attempt to build WordPress as an MVC framework is very suspicious. It is hoped that the implementation of flexibility and efficiency will help the article better understand dependency injection and the pitfalls and benefits of using it. Like all concepts in object-oriented programming (OOP), these concepts should not be used because of established principles. Because they solved the problem, now that we have that problem, we put them
Must be used. More importantly, I hope this helps me think about code architecture in a way that helps me design code that requires the least effort. If the system requires additional post collections, add a few lines of code to the container, but do not override the use below. To expand this container to perform more tasks than the post task requires more new code, but not much change to the existing code. This is what good OOP really means. A maintainable system. You can add maintainable systems without major refactoring, which is what it provides.