SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Am I getting the idea of OOP?

    So, I'm slowly starting to delve into the world of OOP in PHP. It's really a whole new way of thinking isn't it? But I am seeing the benefits, slowly but surely...

    Right, I'm writing this stuff below, so I can make sure I'm learning the terms correctly. Please tell me if I'm wrong, correct or amend where need be, and please tell me what else I should know, so I can make sure I'm getting a firm grasp with all this stuff!

    Class: This is basically a blueprint of my object I want to create. It contains the properties and the actions of what my object can do.

    Object: This is the class blueprint put into action, in a form that can be used in the code.

    Property: This is a property of a class. Say the class was a blueprint for a human being. The properties would be such like hair, height, name etc. This is done using var $[variable name].

    Function/Method: This is a action that the object can perform. These functions/methods can be accessed by using $[class name]->[function name]([parameter one],[parameter two], etc.)

    Constructor: This is the default function/method that gets called as soon as an instance of an object is made. Parameters can be sent to this too, if need be.

    Instantiation: This is when a instance of a object is made. This is like actually 'building the house' (object) from the 'house blueprints' (class). This is done by using $[variable name] = new [class name]([parameters]).

    Initialization: This is when a value is first assigned to a variable.


    How am I doing peeps?
    - Little Jim

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are you learning on PHP4 or PHP5 ?

    What are you learning from, a book?

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you got it about right. Just a few quick notes;

    Property would normally be used to describe a concrete variable on an object instance, since the class doesn't really have any properties (as you said, it's just the declaration or blueprint for the object). This is in contrast to the methods, which actually belongs to the class (Although, the distinction is blurry, especially in a language like PHP).

    Sometimes member is used as a collective for methods and properties.

    Classes can btw. actually have properties, if they are declared static. This is more of a hack, than anything, so you shouldn't use that feature if you want to write good code. Classes can have static methods too, which should be equally avoided (Although they aren't nearly as disruptive as static properties).

  4. #4
    Maniacally depressed robot poncho's Avatar
    Join Date
    Dec 2004
    Location
    Belfast, N.Ireland
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, that all looks cool to me

    Here's a PHP4 class I knocked together to give you an example to work with:

    PHP Code:
    <?php
    // Person Class
    class Person 
    {
        function 
    Person (&$params = array())
        {
            foreach(
    $params as $key => $val)
            {
                
    $this->$key $val;
            }
        }
        
        function 
    setProp($param$value)
        {
            
    $this->$param $value;
        }
        
        function 
    getProp ($param)
        {
            return !empty(
    $this->$param) ? $this->$param null;
        }
    }
    ?>

    <?php
    $batParams 
    = array(
        
    'name' => 'Bruce Wayne',
        
    'alias' => 'Batman',
        
    'job' => 'millionaire philanthropist',
        
    'recreation' => 'vigilante crimefighter'
    );

    $alfParams = array(
        
    'name' => 'Alfred',
        
    'job' => 'butler'
    );

    $batman = new Person($batParams);
    $alfred = new Person($alfParams);
    $batman->setProp('butler'$alfred);
    ?>

    <pre><?php print_r($batman); ?></pre>

    <p><?php echo $batman->getProp('name'); ?> job is: "<?php echo $batman->getProp('job'); ?>"</p>
    I recommend you get a hold of Harry Fueck's PHP Antholoy, it's a great book that introduced me to OO programming, it will stand you in good stead.

    Cheers;
    Poncho
    Perfecting the art of breaking stuff.
    Check 'em: CakePHP | TextMate

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your comments. I'm glad I'm getting at the least the basic idea!

    Cheers!
    - Little Jim


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
  •