{"id":228858,"date":"2022-11-04T17:04:00","date_gmt":"2022-11-04T14:04:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=228858"},"modified":"2022-11-09T04:33:13","modified_gmt":"2022-11-09T01:33:13","slug":"rapid-prototyping-introduktion-av-autoloading","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/sv\/rapid-prototyping-introduktion-av-autoloading\/","title":{"rendered":"Rapid Prototyping: Introduktion av Autoloading"},"content":{"rendered":"\n<p>Under de senaste inl\u00e4ggen har jag g\u00e5tt igenom processen att ta en id\u00e9 fr\u00e5n id\u00e9 till prototyp.<\/p>\n<p>Visst, det finns n\u00e5gra saker som du kan \u00e4ndra (och det finns n\u00e5gra saker jag skulle \u00e4ndra n\u00e4r det g\u00e4ller att organisera klasser). Men po\u00e4ngen med serien \u00e4r inte att g\u00e5 igenom alla de olika objektorienterade teknikerna som kan anv\u00e4ndas f\u00f6r att skapa en l\u00f6sning.<\/p>\n<p>Ist\u00e4llet handlar det om att ta en prototyp och g\u00f6ra om den till n\u00e5got lite mer professionellt. Det finns fortfarande en sak som vi m\u00e5ste g\u00f6ra, dock.<\/p>\n<p>Just nu inkluderar vi alla v\u00e5ra filer genom kravutl\u00e5tanden. Och det h\u00e4r fungerar bra f\u00f6r sm\u00e5 filer, men att inf\u00f6ra autoloading i kombination med namnutrymmen kan g\u00f6ra applikationen \u00e4nnu renare.<\/p>\n<p>Och det \u00e4r vad vi ska g\u00f6ra.<\/p>\n<h3>Vi presenterar Autoloading<\/h3>\n<p>Minns fr\u00e5n ett tidigare inl\u00e4gg (om du har l\u00e4st det), att autoladdning \u00e4r en enkel id\u00e9:<\/p>\n<p>Det l\u00e5ter bra, eller hur? Men det finns en varning: Du m\u00e5ste skriva autoloader. PHP kan inte lista ut det p\u00e5 egen hand.<\/p>\n<p>Och det \u00e4r d\u00e4rf\u00f6r jag \u00e4r ett fan av att se till att v\u00e5ra namnomr\u00e5den ocks\u00e5 f\u00f6ljer en konsekvent katalogstruktur: Om du analyserar namnomr\u00e5det och namnet p\u00e5 filen \u00e4r det relativt enkelt att bygga s\u00f6kv\u00e4gen till filen och automatiskt ladda den.<\/p>\n<p>L\u00e5ter det f\u00f6rvirrande? L\u00e5t oss f\u00f6rst ta en titt p\u00e5 var vi ska placera autoloadern ang\u00e5ende v\u00e5r katalogstruktur:<\/p>\n<p>Uppdatera sedan din plugins bootstrap-fil s\u00e5 att den bara <a href=\"https:\/\/gist.github.com\/tommcfarlin\/4f5087613f01dcb35b7fb5e79c69401d#file-00-plugin-bootstrap-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">kr\u00e4ver autoloader-filen<\/a>.<\/p>\n<pre><code>&lt;?php\n\/**\n * Three Recent Posts\n *\n * @package     TRP\n * @author      Tom McFarlin\n * @copyright   2017 Tom McFarlin\n * @license     MIT\n *\n * @wordpress-plugin\n * Plugin Name: Three Recent Posts\n * Plugin URI:  https:\/\/tommcfarlin.com\/three-recent-posts\/\n * Description: Displays the three mot recent posts in your post editor screen.\n * Version:     1.0.0\n * Author:      Tom McFarlin\n * Author URI:  https:\/\/tommcfarlin.com\n * Text Domain: three-recent-posts\n * License:     GPL\n * License URI: http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\n *\/\n\nnamespace McFarlinTRP;\nuse McFarlinTRPDisplayMeta_Box;\n\n\/\/ Import the autoloader.\ninclude_once 'Includes\/autoload.php';\n\nadd_action( 'add_meta_boxes', __NAMESPACE__. 'trp_start' );\n\/**\n * Starts the plugin.\n *\/\nfunction trp_start() {\n\n    $meta_box = new Meta_Box( dirname( __FILE__) );\n    $meta_box-&gt;init();\n}\n<\/code><\/pre>\n<p>Sj\u00e4lvklart beh\u00f6ver vi autoloadern f\u00f6r att ladda v\u00e5ra filer. I huvudsak fungerar det s\u00e5 h\u00e4r:<\/p>\n<ol>\n<li>separera komponenterna i det inkommande filnamnet (som katalogs\u00f6kv\u00e4gen och filnamnet),<\/li>\n<li>ers\u00e4tt eventuella understreck med bindestreck f\u00f6r att se till att filen f\u00f6ljer strukturen f\u00f6r klassens filnamn,<\/li>\n<li>b\u00f6rja bygga det fullt kvalificerade s\u00f6kv\u00e4gsnamnet (som \u00e4r katalogen, filnamnet, etc.),<\/li>\n<li>iterera genom namnomr\u00e5det ner till klassnamnet och bygg ett fullst\u00e4ndigt kvalificerat namn till filen,<\/li>\n<li>inkludera det i projektet.<\/li>\n<\/ol>\n<p>Det \u00e4r r\u00e4tt fram, eller hur? Om du f\u00f6ljer ett konsekvent namnomr\u00e5de, katalog, klassnamn och filnamnsschema kan du \u00e5teranv\u00e4nda samma autoloader i alla projekt.<\/p>\n<p><a href=\"https:\/\/gist.github.com\/tommcfarlin\/4f5087613f01dcb35b7fb5e79c69401d#file-00-autoload-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">H\u00e4r \u00e4r en kopia av en autoloader<\/a> som jag anv\u00e4nder och sl\u00e4pper den i n\u00e4stan alla projekt jag anv\u00e4nder. Du kommer att se att det f\u00f6ljer exakt vad som beskrivs ovan:<\/p>\n<pre><code>&lt;?php\n\/**\n * Automatically loads the specified file.\n *\n * @package McFarlinTFP\n *\/\n\nnamespace McFarlinTFP;\n\n\/**\n * Automatically loads the specified file.\n *\n * Examines the fully qualified class name, separates it into components, then creates\n * a string that represents where the file is loaded on disk.\n *\n * @package McFarlinTFP;\n *\/\nspl_autoload_register(function( $filename) {\n\n    \/\/ First, separate the components of the incoming file.\n    $file_path = explode( '', $filename );\n\n    \/\/ Get the last index of the array. This is the class we're loading.\n    if (isset( $file_path[ count( $file_path) - 1 ])) {\n\n        $class_file = strtolower(\n            $file_path[ count( $file_path) - 1 ]\n        );\n\n        \/**\n         * The classname has an underscore, so we need to replace it\n         * with a hyphen for the file name.\n         *\/\n        $class_file = str_ireplace( '_', '-', $class_file );\n        $class_file = \"class-$class_file.php\";\n    }\n\n    \/**\n     * Find the fully qualified path to the class file by iterating through the $file_path array.\n     * We ignore the first index since it's always the top-level package. The last index is always\n     * the file so we append that at the end.\n     *\/\n    $fully_qualified_path = trailingslashit(\n        dirname(\n            dirname( __FILE__)) );\n\n    \/**\n     * We start at the second index of the namespace because our directory\n     * structure does not include 'McFarlin\/TRP'.\n     *\/\n    for ($i = 2; $i &lt; count( $file_path) - 1; $i++) {\n\n        $dir = strtolower( $file_path[ $i ] );\n        $fully_qualified_path .= trailingslashit( $dir );\n    }\n    $fully_qualified_path .= $class_file;\n\n    \/\/ Now we include the file.\n    if (file_exists( $fully_qualified_path)) {\n        include_once( $fully_qualified_path );\n    }\n});<\/code><\/pre>\n<p>Och med detta \u00e4r plugin inslagen till version 1.0.0. Du kan <a href=\"https:\/\/github.com\/tommcfarlin\/three-recent-posts\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">kolla in det p\u00e5 GitHub<\/a> (b\u00e5de bokstavligen och bara f\u00f6r recension). Och f\u00f6rhoppningsvis har detta hj\u00e4lpt med att g\u00e5 igenom processen att g\u00e5 fr\u00e5n snabb prototyping till koncept inom konceptet WordPress.<\/p>\n<h2>Serie inl\u00e4gg<\/h2>\n<ol>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/sv\/snabb-prototyping-med-wordpress-fraan-koncept-till-plugin\/\" title=\"Snabb prototyping med WordPress: fr\u00e5n koncept till plugin\">Snabb prototyping med WordPress: fr\u00e5n koncept till plugin<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/sv\/snabb-prototyping-med-wordpress-konceptanalys\/\" title=\"Snabb prototyping med WordPress: konceptanalys\">Snabb prototyping med WordPress: konceptanalys<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/sv\/rapid-prototyping-prototype-to-code-del-1\/\" title=\"Rapid Prototyping: Prototype To Code, del 1\">Rapid Prototyping: Prototype To Code, del 1<\/a><\/li>\n<li><a href=\"https:\/\/wordpress.mediadoma.com\/sv\/rapid-prototyping-prototype-to-code-del-2\/\" title=\"Rapid Prototyping: Prototype To Code, del 2\">Rapid Prototyping: Prototype To Code, del 2<\/a><\/li>\n<li><a href=\"https:\/\/tommcfarlin.com\/introducing-autoloading\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">Rapid Prototyping: Introduktion av Autoloading<\/a><\/li>\n<\/ol>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">Inspelningsk\u00e4lla:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Att inf\u00f6ra automatisk laddning i samband med namnutrymmen i ett WordPress-plugin kan g\u00f6ra applikationen \u00e4nnu renare.<\/p>\n","protected":false},"author":1,"featured_media":220982,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[848,922,724],"tags":[1173],"class_list":["post-228858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-handledningar","category-oevrig","category-utvecklaren","tag-affiai-sv"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/posts\/228858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/comments?post=228858"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/posts\/228858\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/media\/220982"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/media?parent=228858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/categories?post=228858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/sv\/wp-json\/wp\/v2\/tags?post=228858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}