SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Understanding OOP Basics???

    Hey all,

    I got the PHP Anthology book and LOVE it so far. However, I haven't got very far... I'm struggling in chapter two understanding the basic ideas of OOP. For instance, I'm going to quote some code that Harry used for an example... with questions that I am not understanding. Find ** for questions.
    PHP Code:
    <?php
    // Page class
    class Page {
        
    // Declare a class member variable 
     
    // [color=black]**But why must this be done?[/color]
     
     
        
    var $page;
        var 
    $title;
        var 
    $year;
        var 
    $copyright;
     
        
    // The constructor function --- 
     
    //[color=black]** What exactly does this do [/color][color=black]and why is it important? **[/color]
     
        
    function Page($title,$year,$copyright) {
            
    // Assign values to member variables
            
    $this->page '';
            
    $this->title $title;
            
    $this->year $year;
            
    $this->copyright $copyright;
     
            
    // Call the addHeader() method --- 
     
    //[color=black]**Why is this being called now [/color][color=black]when the function is actully below it?[/color]
     
            
    $this->addHeader();
        }
        
    // Generates the top of the page
        
    function addHeader() {
            
    $this->page.=<<<EOD
    <html>
    <head>
    <title>
    $this->title</title>
    </head>
    <body>
    <h1 align="center">
    $this->title</h1>
    EOD;
        }
        
    // Adds some more text to the page
        
    function addContent($content) {
            
    $this->page.=$content;
        }
        
    // Generates the bottom of the page
        
    function addFooter() {
            
    $this->page.=<<<EOD
    <div align="center">&copy; $this->year $this->copyright</div>
    </body>
    </html>
    EOD;
        }
        
    // Gets the contents of the page
        
    function get() {
            
    // Keep a copy of the page with no footer
     
    // [color=red][color=black]Still not understanding what this is doing?[/color][/color]
     
     
            
    $temp $this->page;
            
    // Call the addFooter() method
            
    $this->addFooter();
            
    // Restore $page for the next call to get
            
    $page $this->page;
            
    $this->page $temp;
            return 
    $page;
        }
    }
    // Instantiate the page class
    $webPage=new Page('As Easy as it Gets',date('Y'),'Easy Systems Inc.');
    // Add something to the body of the page
    $webPage->addContent(
        
    "<p align=\"center\">It's so easy to use!</p>\n" );
    // Display the page
    echo ( $webPage->get() );
    ?>
    I'm really wanting to learn all of this, but am having a hard time understanding some basics.

    THANKS!
    Josh

  2. #2
    Non-Member
    Join Date
    Nov 2003
    Location
    here
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats the same stuff that had me confused, also I could never work out why $this was used to call methods lol.

  3. #3
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hear you. I love the Anthology book... but it's almost as if I need a manual for this manual. It's written extremely well, but I'm afraid it's using terms I am not familiar with and I'm having to do some extra digging and re-reading to follow it.

    This OOP stuff is hard!!!

    Anyone else help?

    Thanks,
    Josh

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you should understand that a class is not an object. A class is a kind of blueprint for what an object will be once you instantiate(create) it in your program.

    Object Oriented concepts revolve around modeling "real" life stuff. For instance in Harrys example code your creating a blueprint for a page. A page is a real world object.

    So an object has some characteristics right? Like it has content, a title, etc. These are your class variables. They describe the object.

    Now because the class is just the blueprint it really only is a representation, but when you create an object it should have substance so you have a constructor. The constructor initializes the variables of the object. Sometimes just to empty strings or arrays but sometimes you'll want to initialize an object more specifically if it should start in a certian state of existance.

    In a class you dont have to declare a function before it gets used. A class and its blueprint exist as soon as you include the class file or in this case declare the whole thing. But its not concrete or real until you instantiate it and create an object (with the new operator). This means that PHP understands there is an addHeader function because of the class declaration before it uses the function when you actually create a page object with new.
    Erh

  5. #5
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi jpfour32, Classes can be kinda hard to get your head around ... Maybe this article can help you a bit. Its pretty detailed and gives a thurough explination and example useage..

    http://www.purephotoshop.com/article/71

    -Good luck

  6. #6
    SitePoint Zealot rae's Avatar
    Join Date
    Apr 2003
    Location
    bedroom :P
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    // Page class
    class Page {
    // Declare a class member variable
    // **But why must this be done?
    Because if we declare one or more variable here (and not inside a method a.k.a. function) then we can use these variables in any method in the class.

    The keyword "$this" or "$this->" is used for accessing to these variables.
    So we declare a variable:

    var $page

    And then we can access to this variable inside a function (a.k.a. method) like:

    $this->page = 53;

    or another example:

    print($this->page);

    And I must mention that with the "$this->" keyword, you can access to the methods (a.k.a. functions) of that class.
    So you can write:
    $this->is_a_method_in_my_class();
    And if you write that, then the code after the "function is_a_method_in_my_class() { ... }" will be executed.
    So you can use that to access to your methods in the class, and to access to your variables in the class.
    ...
    ...

    // The constructor function ---
    //** What exactly does this do and why is it important? **
    It's your choice if you want to use this or not. It's a design decision.
    If you write a Constructor, then after when you making an instance of the class the commands in the Constructor method will be evaluated.
    For example you write that:

    $mypage = new Page;

    and then the commands in the "function Page" will be evaluated.
    It's a method which will automatically run, after you made an instance of the class.

    //**Why is this being called now when the function is actully below it?
    Because in PHP, you don't need to write first the function declarations.
    You can write them before and also after the calling. It gives you more freedom.

    // Still not understanding what this is doing?
    I think now you'll figure it out.


    Hope that helped something. But you can easily learn a lot if you search on the net about OOP in PHP
    d(o.O)b -- Notice: undefined variable: /me

  7. #7
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to expand on the constructor. A constructor is a method that is automatically called when you first instantiate an object. In other words, if you created a database class, you could define the constructor to make a connection to the database. However, you don't NEED to define a constructor for this purpose. You could just as easily create a seperate method for making a connection inside the class. However, since the main purpose of a database class would be to handle database connections, this would be a good example of a reason to create a constructor. After all, whats the point of create a database class if you aren't going to make a connection when you instantiate the object? =)
    Messiah | Ink-Press: web publishing simplified!

  8. #8
    SitePoint Addict
    Join Date
    May 2002
    Location
    Louisville, KY
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet... Thanks a ton!! Caged, I read the article and found another one too that has helped a lot:

    http://www.liquidpulse.net/articles/125

    Rae, I really appreciated your help in detail. It answered so many questions I've had about OOP. Slowly and surely I'm getting a grip on this stuff!

  9. #9
    SitePoint Zealot rae's Avatar
    Join Date
    Apr 2003
    Location
    bedroom :P
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jpfour23
    Rae, I really appreciated your help in detail. It answered so many questions I've had about OOP. Slowly and surely I'm getting a grip on this stuff!
    We are all glad to help you.
    d(o.O)b -- Notice: undefined variable: /me

  10. #10
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read everything in the world trying to understand the basics of OOP. Once you understand the basics the rest unfolds. When I got done reading everything I ended up right back here with the gifted one .

  11. #11
    SitePoint Enthusiast DmS's Avatar
    Join Date
    Jan 2004
    Location
    Stockholm, Sweden
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm in the process of writing a tutorial as an introduction to oop, it's not quite finished yet, some zip-files 'n stuff are missing, also it's a follow up to another earlier tutorial so there are some db-code missing as well for the end part. however I think the first parts might explain some.
    You are welcome to the draft: http://www.dmsproject.com/test/gurus...php_mysql.html
    /Dan
    { knowledge is what remains once you forget what you learned }
    Home: DmSProject Tutorials: GurusNetwork
    Committed at:
    OzoneAsylum + Blog


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
  •