Thanks, Weedpacket.

And thanks, rvdavid.

I did notice Harry pushing PEAR at every opportunity. That is a bit disappointing, so I will check out your recommendations.

    5 days later

    Hello again rodney

    I noticed in your first class Code, you added values into your addMenu() function.
    Suppose you want to change your menu, when adding removing pages.
    Is more flexible to make the Class neutral or transparant regarding values.
    Much more useful.

    So I added several different possibiliteies
    to use data from outside this function and also from outside Class.

    // menudata format
    $data = array(
    'value1'=>'display1',
    'value2'=>'display2'   );
    //as in
    <a href="?p=value">display</a>

    I tested this code, and it works.

    <?php
    class Page {
        // declare class members
        var $page;
        var $content;
        var $title;
        var $keywords;
        var $description;
        var $year;
        var $copyright;
        var $menuitems = array('dflt'=>'Home');
        var $menuhtml;
        // generate page navigation
        function makeMenu($currentitems='')
        {
            if(!$currentitems)
                $currentitems=$this->menuitems;
            $this->menuhtml  = '<div id="menu">'."\n";
            $this->menuhtml .= '<ul>'."\n";
            foreach($currentitems as $key => $val){
                $this->menuhtml .= '<li><a href="?p='.$key.'">'.$val.'</a></li>'."\n";
            }
            $this->menuhtml .= '</ul>'."\n";
            $this->menuhtml .= '</div>'."\n";
        }
    }
    
    /////////////  Usage  //////////////////////////
    // test1, takes default value of $this->menuitems and display
    $Out = new Page;
    $Out->makeMenu();
    echo $Out->menuhtml . "<hr>\n";
    
    // test2, setting $this->menuitems and display
    $Out->menuitems=array(
    '1'   =>'One',
    '3'   =>'Three',
    '4'   =>'Four',
    'dflt'=>'Home');
    $Out->makeMenu();
    echo $Out->menuhtml . "<hr>\n";
    
    // test3, submitting values directly to function
    $mymenu['2']='second';
    $mymenu['1']='first';
    $Out->makeMenu($mymenu);
    echo $Out->menuhtml . "<hr>\n";
    
    // test4, shows $this->menuitems is left intact = test2
    $Out->makeMenu();
    echo $Out->menuhtml . "<hr>\n";
    
    // test5, $this->menuhtml can of course be used again
    echo $Out->menuhtml . "<hr>\n";
    
    ?>

      halojoy,

      <off topic>It is good to see you. Long time. How are you? Hope all is well in Sweden. I have been working in PHP very diligently and have learned much since I first came to the forums almost a year ago now... </off topic>

      Thanks for the suggestion. I have reread the manual in regards to OOP and have been considering all the suggestions in this thread so far... I am sure it will be much better than I originally posted as I am learning very much about objects. I like the flexibility and modularization of code so I can quickly reuse code/functions->object classes that have already been written by myself or others. Very, very cool stuff.

      Thanks again!

        Rodney H. wrote:

        halojoy,

        <off topic>It is good to see you. Long time. How are you? Hope all is well in Sweden. I have been working in PHP very diligently and have learned much since I first came to the forums almost a year ago now... </off topic>

        Thanks for the suggestion. I have reread the manual in regards to OOP and have been considering all the suggestions in this thread so far... I am sure it will be much better than I originally posted as I am learning very much about objects. I like the flexibility and modularization of code so I can quickly reuse code/functions->object classes that have already been written by myself or others. Very, very cool stuff.

        Thanks again!

        I am posting elsewhere now.
        But still writing PHP all the time .. right now 2-3 applications working on.

          even if there is too much php to learn for anybody in a lifetime
          you are absolute no beginner no more

          Is this really a creation of yours?

          My PHP Notebook
          Lessons In PHP & Code Samples

          http://www.php-lessons.com/?page=index

          a lot good codes and functions - snippets - to learn from
          🙂

            halojoy wrote:

            Is this really a creation of yours?

            If you are talking about the class, it is an expansion from one example in Harry Feuck's book....

            Thanks!

              no,
              of course i am talking about php-lessons link

                I would call that my PHP-toy, Halojoy.

                I built it after thinking about how you always said flatifiles rule... so i made a bunch of flat files using the things I was learning about PHP as the subject...

                it was fun LOL!

                  6 days later
                  JordanL wrote:

                  As a procedural guy, I'm sure that you used functions a few times. Like, for instance, a "makeSafe()" function to clean user input, or a "dbconnect()" function for connect to multiple databases.

                  Objects are like taking a group of functions that perform similar tasks, and letting them know they're related. For instance, let's say that you are building a script to edit a config file. You'd want one function to addconfig(), one to editconfig and one to deleteconfig() at the very least. However, all of these functions have a few things in common. First, they are all dealing with the config file. Second, they are all going to be dealing with a similar sort of input structure, (i.e. $config_name, $config_value).

                  You could do things the procedural way and just write three complete functions, but with an OOP method, we can let these functions know they perform similar tasks by putting them in a class. This allows us to pull out common threads between them, such as the location of the config file, and make them variables which are shared only among these similar functions.

                  ............

                  Thank you, thank you, thank you! I have tried to gain a clear concept of the reason for using objects over plain functions, and your example makes it as clear as day! I think the problem with learning OOP is that most programmers have already become so accustomed to the idea of using functions to create blocks of code that they have such a hard time understanding how classes and objects actually work, and why they should be used. As you said, there are all kinds of words used to explain what something is, but the word used needs an explanation itself. An endless loop appears and it becomes very frustrating.

                  I found this post while Googling for some OOP tutorials and was so happy to find such a perfect example of classes and objects that I registered a PHPBuilder forum account just to post this reply. There really needs to be more OOP examples created from a procedural, function perspective, instead of trying to reteach from scratch how code should be structured using OOP.

                  Anyhow, my two cents. Thanks again JordanL!

                    Welcome to PHPBuilder, JordanL. Hopefully we will se more of you with some examples of OOP while you are learning, too. Check out one of the other forums, called Echo Lounge, for a new general discussion about HOW people are/have learned OOP. Jason Batten has mentioned a couple of really great books/references.

                    Thanks.

                      4 days later

                      UPDATE

                      Yesterday I started working on a calendar that will pull events from a database.

                      After I had written about seven or eight functions at the beginning, I put them on a separate file.

                      Then guess what? I spontaneously turned the functions into methods in a class. The first one I wrote without an example to follow from a tutorial... I just did it.

                      Woo hoo.....

                        Write a Reply...