MySpaza - How To
Web Design Tips and PC and Linux Related Information

Subscribe
 



    follow me on Twitter

    PHP OOP - The basics of php objects
    php objects, php oop, simple php objects, easy php oop

    Added: 2009-01-04 00:43:19

    PHP's OOP is a useful tool, this article will present the basics of PHP OOP, from creating a new class to creating your first method.

    First use the class keyword to contain your class methods and properties.

    <?

    class MyNewClass (

    # Class properties and methods

    )

    ?>

    Ok so thats how to create the class container so what about the methods and properties.

    First we will define some basic properties using the private keyword, the private keyword defines the properties as private to the class, therefore the property cannot be accessed from outside of the object, or from any class extensions.

    <?

    class MyNewClass (

    private $propertie;

    # Now we will use the __constructor keyword to
    # make the object operate
    # The constructor method is run when the object is instatiated.

    function __constructor($someVariable) {

    $this->propertie = $someVariable;

    $this->output();

    }

    # Now we create a function to output the above object property
    #  using the print command.
    # The following function is called in the __constructor function
    # above with $this->output();

    private function output() {

    print $this->propertie;

    }

    )|

    # Now to instatiate the class

    new MyNewClass('Easy PHP objects');

    ?>

    The output of the above script would be

    Easy PHP objects

    This is a simple example of creating an object with a methods and properties.

     




    Add a comment to this post
    Email confirmation is required for each post

    Name:
    Email:
    Comment:
     

    Comments from other users:




    Copyright MySpaza.Co.Uk - 2010