SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comments on a Links Class Please

    Hello all:

    I'd like to make sure that I am grasping and correctly implementing OOP in PHP.

    Would you provide some feedback on this Links Class I have built as part of an application I am working on?

    The comments on the class explain it a bit. It gives a template class a single place to make and access links for the application.

    PHP Code:
    <?php
    /**
     * Provides a single place to make and keep all URLs for the application.
     * 
     * All the URLs (Links to Page Views) for the application are contained in this class.
     * They are built using the factory method MakeLinks().  
     * Right now, the application checks modes and actions by the URL query string.  
     * So URLs are built like:
     * <code>www.site.com/default.php?mode=SignInPage&action=showForm</code>
     * If I decide to use mod rewrite to allow my URLs to look like:
     * <code>www.site.com/SignInPage/showForm/</code>
     * Then I just need to change my MakeLinks method or overide it in a child class.  
     * 
     * This class is used by the Template class to access the application's URLs and thier pieces.
     * Templates my have image based nav bars or text based hyper links.  This provides a way to
     * put into the template whatever part of the URL is needed.  See the class test below.
     * 
     */
    class Links {
        
        
    // DATA MEMBERS
        /***
         * The default URL to self.  Usually PHP_SELF.
         * @type string
         ***/
        
    var $mPhpSelf;

        
    /**
         * Links::Links()
         * Constructor sets PHP_SELF.
         * @return void
         **/
        
    function Links ()
        { 
            
    // Set the link to the self page by using $_SERVER['PHP_SELF']
            // This could also be set by just putting the default file name.
            
    $this->mPhpSelf $_SERVER['PHP_SELF']; 
        } 
        
    // Accessors
        /*
         * Links::HomePage()
         * 
         * @param string $linkType The type (piece of) link that the template wants. 
         * @return string
         **/
        
    function &HomePage ($linkType ='')
        {
            return 
    $this->MakeLinks('Home Page'''''$linkType); 
            
    //Unlike the other links, no mode is needed.  
            //The application will show a default mode when no mode is detected.
        


        
    /*
         * Links::SignInPage()
         * 
         * @param string $linkType The type (piece of) link that the template wants. 
         * @return string
         **/
        
    function &SignInPage ($linkType ='')
        {
            return 
    $this->MakeLinks('Sign In''sign_in''' $linkType);
        } 
        
    /*
         * Links::AdminPage()
         * 
         * @param string $linkType The type (piece of) link that the template wants.
         * @return string
         **/
        
    function &AdminPage ($linkType ='')
        {
             return 
    $this->MakeLinks('Go to Your Admin Section','admin'''$linkType);
        } 
        
    /////////////////////////////////
        //The rest of the pages are deleted here to shorten the post.
        ////////////////////////////////
        
        
        
        // Methods
        // Builds a text-based html link with query string parameters of mode and action
        
        /**
         * Links::MakeLinks()
         * Factory method used to make the URL.
         * Although this might normally be a private method, 
         * it may be used outside the class if there was a reason to contruct a URL
         * according to the structure the rest of the URLs use.
         * @param string $linkText The text the link will show
         * @param string $mode The mode to add to the query string
         * @param string $action The action to add to the query string
         * @param string $linkType The type (piece of) link to make.
         * @return string
         **/
        
    function MakeLinks ($linkText ''$mode ''$action ''$linkType ='')
        {
            if (
    $linkType == 't'
            {
               return 
    $linkText
            }
            else
            {
                
    $url $this->mPhpSelf
                
                if (
    $mode != ''
                {
                    
    $url .= '?mode=' $mode
                }
                if (
    $action != ''
                {
                    
    $url .= '&action=' $action
                }
                if (
    $linkType == 'u'
                {
                    return 
    $url;    
                }
                if (
    $linkType == ''
                {
                    
    $link '<a href="' $url '">' $linkText '</a>';
                    return 
    $link;
                }
            }
        } 
        
        



    ?>
    <?php
    /**
     * //Test the class
     * //Uncomment to test.
     * 
     * $link = & new Links();
     * 
     * echo 'The full link: ' , $link->AdminPage();
     * echo '<p>The link Text: ' ,$link->AdminPage('t');
     * echo '<p>Just the URL: ' , $link->AdminPage('u');
     */
    ?>
    Along with your comments, I am interested in:

    1. Is there a better way to do this?
    2. What problems can you see?
    3. Am I correct in calling the MakeLinks() a factory method?
    4. I have read about having methods return by reference. Is this needed here when I return the link strings to the template class? How is this different than if I did not use the return by reference?


    Thanks Alot:
    Sean
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Generally looks like you've grasped OOP nicely and like where you're going with this class - seems like you'd use this in another script to build HTML.

    1. Is there a better way to do this?
    I'm not sure about having a method for each link you want to build otherwise you may end up with a very large class. Depends on the overall plan. For something small this is probably fine and it's good to have the information in the same place accessed throughout your application. Perhaps it might be better to store the links in a database though and provide a general method for fetching them such as getLinkByName('Homepage');.

    2. What problems can you see?
    One thing you might want to avoid is rendering HTML here;

    PHP Code:
                if ($linkType == '')
                {
                    
    $link '<a href="' $url '">' $linkText '</a>';
                    return 
    $link;
                } 
    Better perhaps to return the URL and the linktext rather than formatted HTML.

    3. Am I correct in calling the MakeLinks() a factory method?
    Yes and no. If you were using Java then I guess MakeLinks() returns string objects... In general a factory method builds other objects. For example;

    PHP Code:
    <?php
    class Links {
        var 
    $mPhpSelf;
        function 
    Links ()
        {
            
    $this->mPhpSelf $_SERVER['PHP_SELF'];
        }
        function &
    HomePage ($linkType ='')
        {
            return new 
    Link('Home Page'''''$linkType);
        }
        function &
    SignInPage ($linkType ='')
        {
            return new 
    Link('Sign In''sign_in''' $linkType);
        }
        function &
    AdminPage ($linkType ='')
        {
            return new 
    Link('Go to Your Admin Section','admin'''$linkType);
        }
    }

    class 
    Link {
        var 
    $url;
        var 
    $linktext;
        function 
    Link ($linkText ''$mode ''$action ''$linkType ='')
        {
            
    // some stuff here
        
    }

        function 
    getUrl () {
            return 
    $this->url;
        }

        function 
    getLinkText () {
            return 
    $this->linktext;
        }
    }
    ?>
    The Links::HomePage(), Links::SignInPage() and Links::AdminPage() are now all factory methods which build instances of the class Link. The Link class is intended to do what your MakeLinks method was doing before. Probably it's a good idea to rename the Links class to LinkFactory btw - to me the name Links sounds like a class holding the results from a database query.

    The point with the Link class above is no longer to render HTML - just to manipulate data into a form which is ready to use in the script that you'll build you page with.

    4. I have read about having methods return by reference. Is this needed here when I return the link strings to the template class? How is this different than if I did not use the return by reference?
    In your case I don't think you need to pass by reference - once a link is created my guess is no other class should need to change it. My attempt to explain references is here.

    Hope that helps.

  3. #3
    SitePoint Evangelist worksdev's Avatar
    Join Date
    Mar 2002
    Location
    Central, PA - originally from Monterey, CA
    Posts
    497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks much Harry.

    Thanks also for your PHP Patterns site.

    Best Regards,
    Sean
    Church Website Design by Enlighten Web Services -
    Web design-hosting-support for Christian ministries
    Friendly Community of Webmasters
    Christian Websites :: Christian Web Design


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •