Like all php programmers i have become more than familiar with the functions require, include, and require_once. All are for the inclusion and execution of scripts, or in simple terms a method to include classes or code contained in another file. An issue that faced when using these methods is knowing where to find the files from the current file, and also knowing all the files you need to include.

In php 5 the __autoload function was defined that enables a developer a way to define a function that if a class has not been loaded attempts to load it.  If for example you are creating an application that contains all classes in a folder called class you can define the __autoload function to attempt to require the php file in the classes folder named after the class.  This method works great if you are creating your own app not utilizing additional libraries that may utilize their own file hierarchy and loading scheme as only one __autoload function can be defined.

To enable the usage of multiple autoload functions spl_autoload_register was created.  The purpose of this is to the ability to register multiple functions that are attempted to load the class if it has not been loaded.  The main benefit being that multiple libraries can define their own autoload function and as a developer you only need to register their autoload functions in your files that utilize their libraries.

A sample basic function that can be used to autoload classes is outlined below. If we use the above example that all classes are stored in a file named after the class and contained in a folder called class. Also assume that our main library class is MDLib and autoload is a function defined in it.

public static function autoload($className)
{
    if (class_exists($className, false) || interface_exists($className, false)) {
        return false;
    }
    $class = dirname(__FILE__) . "/class/" . $className . '.php';
    if (file_exists($class)) {
        require $class;
        return true;
    }
    return false;
}

To register the autoload function we would simply need to require the main class then call spl_autoload_register passing the class and function to it.

/* Require Lib Main Class */
require_once(dirname(__FILE__) . '/lib/MDLib.php');
/* Register Auto Loader */
spl_autoload_register(array('MDLib', 'autoload'));

That is all there is to it. For further information you can visit the official php documentation. If you find yourself creating multiple classes for use in a library then using the spl_autoload_register function can save you and your end users time and effort in determining all the classes they need to register, also it could save load time as the classes won’t be loaded until the first time they are used.

, ,

Doctrine is an Object Relational Mapper (ORM) written for the PHP programming language. First off, for those who don’t know, an ORM is programming technique of connecting objects to the data stored in a relational database. What this provides is an object based method of modifying and interacting with the database instead of writing sql queries.

I do not want to go into the details as to the pros and cons of using an ORM but for those of you creating websites and applications that utilize relational databases then utilizing an ORM can save you a lot of grief writing and maintaining sql queries. Some key benefits to using Doctrine are listed below:

  • Search
    Doctrine includes an interface for easily performing searches on specified properties of your objects.
  • Data Validation
    In addition to Database level validation, Doctrine has built in validation so that you will get exceptions at the php level before a DB query is performed.
  • Dynamic Query Language (DQL)
    In addition to utilizing your defined objects for interacting with the data, Doctrine has its own advanced query language to easily pull objects based on its relationships and parameters.
  • Event Listeners
    Doctrine has built in support for listening to events on the query, record, and connection level.  This allows you to easily add in functionality before and/or after  inserts, updates, deletes, etc.
  • Pagination utility
    pagination of data is a common functionality of websites. Doctrine comes with a highly customizable pager package to easily setup pagers within your site.

To learn more about Doctrine head on over to their website, check out the User Documentation,  or jump right in by following along with their My First Project Tutorial.

Search Engine Optimization (SEO) techniques can be categorized as white hat or black hat. White hat optimization is when approved techniques are used to enhance your website’s page rank, I have covered the basics of those in a previous post in this entry am going to focus on black hat or in more forwardly: what a professional Search Engine Optimizer or website admin should not do!

Rule 1: Do not use invisible or hidden content

Invisible Text is the practice of embedding textual content in your site that is not visible to users but instead is placed for the sole purpose of being crawled by search engines. This violates search engines guidelines not to display different content for spiders that a user would not see.

Rule 2: Don’t stuff your keywords

Keyword Stuffing is when you overload your keywords meta data tag with an excessive amounts of keywords that do not match content on your site. Keywords should be used for your pages but should not be abused.

Rule 3: Don’t Cloak

Cloaking is the practice of displaying different content to spiders then what a user would see. This is similar to Invisible Text but instead of some additional content this displays a completely different page that may not be related at all to the real one.

Rule 4: Doorway’s are bad

Doorway Pages are pages built specifically to obtain high page rankings and contain links to other sites that the creator really wants you to visit. These initially may increase your ranking but when found out your page ranking will be affected by being affiliated with the site.

Rule 5: Interlinking is just darn confusing

Interlinking or Link Farming is when a group of individuals come together and incorporate links to each others site with the purpose of increasing each others rankings and not providing relevant content.

Rule 6: Everyone one hates Spam

Spam comes in a multitude of forms including Comment Spam and Spam Pages. Like the name suggest comment spam is the creation of comment on other people’s websites with links to your own site with no relevance to the content of the page you are on. Spam Pages on the other hand are entire pages created with keyword embedded content that provides no real use and contain links to sites that they really want to drive traffic to.

In summary website optimization can be done correctly to realize long term gain or it can be done by violating search engine guidelines to realize short term gain but hurt you over the long term. Like everything else in life I find that real pride and satisfaction comes by doing things right and realizing long term results not just short term ones. If you are serious about your search engine optimization then don’t use black hat techniques.

Overview

A very important element of any website is its navigation or menu, and a good website has a non-obtrusive but prominent one that the user knows intuitively to interact with. A common menu technique I use is having an image as the menu item with a second image as the roll-over state.  To accomplish this I utilize JQuery to modify the DOM based on the user’s interaction. In Part 1 of the guide we will lay the ground work by defining the base elements and javascript that will perform the image swap.

Basic Roll-over Roll-out Set-up

The javascript code we will implement will work within any Document hierarchy that utilizes the navigation element as an image wrapped in an anchor tag, example:

<a href="../about/" ><img src="../img/about_btn.jpg" width="82" height="68" alt="About" /></a>

Using the above as our basic structure we need to add some identification to the link (Anchor tag) that this is a navigation element to do this we will add a class to them of  “siteNav” so our links will now look like:

<a class="siteNav" href="../about/" ><img src="../img/about_btn.jpg" width="82" height="68" alt="About" /></a>

Now that we have identification on the navigation links we can start implementing our javascript. To start with we will utilize the jquery ready function so that our code is called after the page has been loaded and is ready for manipulation. Once the page is loaded we can utilize the identifying class to access and manipulate the navigation items using the each function.

$(document).ready(function () {
    $(".siteNav" ).each(function (i) {
    });
});

Inside the each statement we can set-up functions to fire on mouse events (rollover, and rollout) to simplify it further JQuery has a nice hover function that we will utilize to modify our navigation images. The basic logic we are going to instantiate is that on rollover modify the image url so it shows the over image, and on rollout modify the url so it shows the out or default image. To accomplish this we will name all over images the same as the default image with _over appended to the name. An example is about.jpg and about_over.jpg.

$(document).ready(function () {
    $(".siteNav" ).each(function (i) {
        $(this).hover( function () {
            var img_url = $(this).find("img:last").attr("src");
            $(this).find("img:last").attr("src", img_url.substring( 0, img_url.indexOf( ".jpg") ) + "_over.jpg" );
        },
        function () {
            var img_url = $(this).find("img:last").attr("src");
            $(this).find("img:last").attr("src", img_url.substring( 0, img_url.indexOf( "_over.jpg") ) + ".jpg" );
        );
        });
    });
});

Conclusion

With this set-up we now have a navigation whose items change images when the user rolls over them.  Now to be fully informative and user friendly you will also want to have an active state such that if a user is not interacting with the navigation the link that corresponds to the page they are on will remain in its rollover state. How to accomplish this is outlined in Part 2.

Overview

Search Engine Optimization (SEO) is straightforwardly the modification of your website to make it friendlier to Search Engines with the goal of increasing your page ranking from Organic Search. Unfortunately implementing SEO on a site is not as straightforward as Google and other Search Engines  utilize proprietary algorithms that utilize hundreds of variables to rank your site.  Although SEO is an important step needed be forewarned there is no silver bullet that will get you the #1 ranking and know also that fluffing your site with false content or doorways specifically for Search Engines can and will hurt your ranking and might even get you black listed.

Basic Guidelines

Rule #1 : Create your site for your users and not for search engines.

No matter what anyone tells you your site needs to speak to your visitors with search engines an after thought.  The premises are these if your content is not useful for your visitors as all it does is spit out generic keywords then a user is not going to stay on your site as their is no point.  Create rich, unique, and useful content that users will want, by doing so you may not be seeding organic search on the short term but sites linking to your site will enhance your ranking over the long term.  Seriously you built your site for a purpose make sure you meet your goals.

With that important fact out of the way lets move on to some of the basic SEO techniques.

  • Page Title
    The title of your page should be precise and informative. This tells the user where they are and what they can find on this page.  It is good practice to keep your name in the title with the title of the page. For example if you are a website developer and your company is XYZ corp and your tag line is “The Website Experts” then a reasonable title for the main page could be “XYZ Corp : Thew Website Experts”. However once inside the site the title should be more specific to the content on the page, for example a contact page could be “Contact Us at XYZ Corp” or “XYZ Corp : Contact Us”.

    <title>XYZ Corp: Portfolio of Short Films</title>
  • Keywords
    Keywords is debated as not being used by Search Engines due to their abuse by web sites. However it is a good practice to add keywords to your page as long as they are relevant to your content. The keywords meta tag gets placed in the head tag of your html.

    <meta name="keywords" content="Search engine optimization"/>
  • Description
    The description meta tag is used to place  a short description of the page. It is good practice to utilize keywords as part of your sentences inside this tag.

    <meta name="description" content="Search Engine Optimization (SEO) Tips and Tricks">
  • URL Structure
    The url or the page is displayed in search results and possibly when users link to your site. Making the link user friendly also can notify Search Engines what content to expect on the page.  For example if you are an online store and are selling antique lamps then a posible url for your products could be www.antiquecompany.com/products/lamps/Red_Floral_Shade_Lamp/ This provides the user with information on what they can find on the page instead of a url like www.antiquecompany.com/?cateogry=1&product=237 which tells the user nothing about what to expect on that page.
  • Site Map
    Having a site map for your website can provide users and search engines the ability to readily find pages in your site that is of interest to them.  I highly, highly recommend that you utilize a separate page for this with a link of every page in a utility navigation (not the main nav), please do not add it to the bottom of every page as it just adds clutter to your site.  A site map is a last resort for users to find content, your navigation should readily provide them access to content that they want, if users are repeatedly accessing the site map then you may want to reconsider the structure of your site.
  • Content
    Content is the most important element of your site. Regardless of the above if your site does not provide new, unique, compelling, and above all else useful content a visitor will not stay. A visitor to your website is there for a purpose and your site should provide the information they are looking for. If you simply regurgitate information without providing anything new or different then users will not revisit your site.  When writing content keep in mind the keywords that users could be looking for and add those in to the content if appropriate. Do not force them in though having good content regardless of keywords will lead to visitors which could lead to links to your site and content increasing your ranking.

    • Anchor Text
      Anchor tags or a tags provide links to other sites. Utilize descriptive text instead of click here or similar general text so that users and search engines to understand what the page you are linking to is about.
    • Heading Tags
      Heading Tags or h1, h2 tags are meant to be used to specify the importance of content use them as such.  Place titles in header tags so that users and search engines realize their importance.
    • Images
      Search Engines can not view images instead the utilize the image name and alt property to gather information on what the image is.  Utilize short but descriptive names and place a description of the image in the alt tag as this will be displayed to users if the image can not be loaded.

Conclusion

Search Engine Optimization is a fickle topic, with each Search Engine utilizing their own algorithms and variables. The simplest and most often forgotten rule is simple build your site and write content for your users.  As for the rest be wary of SEO professionals who guarantee results as there are numerous “black hat” methods used that can get your black listed and removed from results.  Simply provide valuable and clear content following the basic guidelines is more than enough for most websites.

, ,