Understanding Classes and OOP

Devolio

Search

What I'm Doing

    Wednesday, November 28. 2007

    Understanding Classes and OOP

    Knowing what functions are is pretty common knowledge to most programmers, as well as the idea of Object Oriented Programming, or OOP. But the move from procedural code to object orientation can be daunting to some at first, to say the least. Even back when I was new to programming, the idea of objects instead of structured procedures was an odd mindset, and hard to wrap my head around. This is a guide to understanding the basics of classes and OOP in concept, and in practice within PHP.

    Functions and Classes

    The relationship between functions and classes, though somewhat complex, can be described (by myself,) as follows:

    A class is a group of functions that work together, and can hold a specific data model with functions to perform operations on the data models. Like functions, classes can be called whenever needed during any page which the class is included. The advantages are numerous, but the big one is efficiency gained, as long as you only instanciate and run new objects as needed, your code will run a lot quicker. To begin, let's start off with a simple example of a class with a model, and a function.
    class aboutme
    {
    public $name = 'Joey';
    public function _get_name()
    {
    return $this->name;
    }
    }

    In the class aboutme, there is a public variable ($name) with the value 'Joey', and a function to get the name. Now that we have the class done, let's look at the various ways to use classes.

    Using Classes

    Any time you have a class included in your page, you can run an instance of it. There are a few different ways to run them.
    echo "My name is ".aboutme::_get_name();
    // class::function()

    Will do the same thing as...
    $me = new aboutme();
    // $var = new class()
    echo "My name is ".$me->_get_name();
    // $var->function()

    Visibility

    Now, you probably noticed that we used the word public for our variable and function. That would be a problem in an application that needs to be secure, because setting our variable and function as public would allow the data to be publicly readable and writable. The three visibility settings are:

    public - Anybody can read and write to this variable or call this function. It can be used by your own and external code sources.

    private - Only code in the same class may read or write to this variable or call this function. It can be used by your own code only, and not from external sources.

    protected - Only code that is your own, or extends the functions of this class can read or write to this variable or call this function. It can be used by your own code, and by external sources.

    Constructing and destructing instances

    Now that we have that out of the way, let's talk about cleaning up our code properly. Any time you load an instance of an object, you should use a __construct function and __destruct to prepare and destroy all of the used variables. For instance...
    class aboutme
    {
    protected $name = 'Joey';
    public function __construct()
    {
    // construct variables
    }
    public function __destruct()
    {
    // cleanup code here
    }
    public function _get_name()
    {
    return $this->name;
    }
    }

    Const and static

    Within classes, you can use $this-> to use variables or methods within the same class. But because my name doesn't change, we should make it static. To add static data to classes, you can use constants, or static variables or functions. To define a constant we would use...
    class aboutme
    {
    const name = 'Joey';
    public function __construct()
    {
    // construct variables
    }
    public function __destruct()
    {
    // cleanup code here
    }
    public function _get_name()
    {
    return aboutme::name;
    }
    }

    However, constants are readable by everyone, so we will change that in a second. To use constants, you can use them in the global scope (::) you would use (aboutme::name). To control the visibility on static data, you can add the word static before the variable or function name, like so:
    class aboutme
    {
    protected static $name = 'Joey';
    public function __construct()
    {
    // construct variables
    }
    public function __destruct()
    {
    // cleanup code here
    }
    public function _get_name()
    {
    return aboutme::$name;
    }
    }

    And to call them, use a $ before the static variables. Let's recap.

    Recap

    - Classes are functions that work together with custom data models.
    - Visibility settings for public, private, protected variables and functions.
    - Use __construct and __destruct to prepare the data model and clean up after it.

    When to use ::
    When calling a variable or function in a class globally (anywhere,) or when what you're referencing is a const or static.

    When to use ->
    When calling a variable or function in a class that is not constant or static, and to reference variables or functions with a variable.

    When to use $this->
    When referencing a variable or function from within the same class that is not const or static.

    When to use self::
    When referencing a const or static variable from within the same class.

    And that concludes part one. In part two we'll be covering extending classes, implementing, abstracting, more OOP concepts, and patterns like MVC. Have a question, or want to add your two cents? Leave a comment.

    Trackbacks

    No Trackbacks

    Comments
    Display comments as (Linear | Threaded)

    No comments


    Add Comment

    Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
    Standard emoticons like :-) and ;-) are converted to images.
    E-Mail addresses will not be displayed and will only be used for E-Mail notifications