{"id":18,"date":"2008-02-04T15:47:57","date_gmt":"2008-02-04T15:47:57","guid":{"rendered":"http:\/\/muratyaman.co.uk\/wp\/?p=18"},"modified":"2020-04-01T12:58:57","modified_gmt":"2020-04-01T11:58:57","slug":"php5-autoloading-your-magic","status":"publish","type":"post","link":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/2008\/02\/php5-autoloading-your-magic\/","title":{"rendered":"PHP5: Autoloading your magic!"},"content":{"rendered":"<p>__autoload() function can be very useful to <strong>optimize <\/strong>your code esp. when you have so many classes.<\/p>\n<p><strong>Unlike class extensions, optional parameters with class restrictions may not load your class.<\/strong><\/p>\n<pre lang=\"php\" line=\"1\">\r\n<?php\r\nclass bClass{\r\n  function fun($p1, aClass $p2=NULL){\r\n    \/\/do something\r\n  }\r\n}\r\n\r\n\/\/depending on the usage\r\n$b = new bClass();\r\n$b->fun('No!');\/\/this will not load class file for aClass\r\n$b->fun('Really?', new aClass('Yes!'));\/\/this will\r\n?>\r\n<\/pre>\n<p>So, it&#8217;s very encouraging to use classes everywhere!<br \/>\nEven encapsulating your functions inside simple classes to use like static modules, will help a lot!<\/p>\n<p>Let&#8217;s say, you have <b>50k<\/b> lines of code inside <b>100<\/b> classes\/files.. If you need a simple task to do very quickly, you should not be loading all of those files, except the ones you need.<\/p>\n<p>Though, it may be dangerous on some cases regarding the dependencies, load order, etc. Carefully design your classes.<\/p>\n<p>Check this: <a href=\"http:\/\/uk.php.net\/manual\/en\/language.oop5.autoload.php\">php.net\/manual\/en\/language.oop5.autoload.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>__autoload() function can be very useful to optimize your code esp. when you have so many classes.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[65,26],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-technology","tag-oop","tag-php"],"_links":{"self":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":3,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":988,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/988"}],"wp:attachment":[{"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.muratyaman.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}