noloh Logo SmallDeveloper Zone
Username:
Password:
remember meforgot password?Login
twitterfacebookirc
plusfolder closePrinciples
plusdocumentWhat is NOLOH?
plusdocumentWhat's so Bad About Markup?
plusdocumentDevelopment and Philosophy
plusfolder closeFeatures
plusdocumentSyntactical Sugars
plusdocumentMultiple Inheritance
plusdocumentBookmarks and NOLOH
plusdocumentUser State Management
plusdocumentSearch Engine Friendly
plusfolder closeGetting Started
plusdocumentPHP and NOLOH Syntax
plusdocumentHello World
plusdocumentConstructors
plusdocumentInstalling NOLOH
plusfolder closeCrash Course
plusdocumentNOLOH and CSS
plusdocumentLayout in NOLOH
plusdocumentDatabases
plusdocumentData::$Links
plusdocumentEvents in NOLOH
plusdocumentMoving and Resizing Your Objects
plusdocumentMultiple Inheritance
plusdocumentBookmarks and NOLOH
plusdocumentData Binding
plusfolder closeAdvanced Topics
plusdocumentCustom Events
plusdocumentClientside Functions
plusfolder closeSyntactic Sugars
plusdocumentSyntactical Sugars
plusdocumentCascading
plusfolder closeCoding Guidelines
plusdocumentBest Practices
plusdocumentNOLOH Naming Conventions
Constructors
Constructorsoverviewdiscussion

In object-oriented programming, a constructor is a function that is called when an object is created. A constructor is similar to a normal function, except that it doesn't return anything, and can not be inherited. In many languages, constructor methods have the same name as the class. In PHP, it's possible to declare a constructor two different ways.

NOLOH declares constructors using the common practice of naming them the same as their class.

The traditional way to declare a constructor:

class SomeObj
{
    //This is the constructor for SomeObj
    function SomeObj()
    {
    }
}

An alternative way to declare a constructor in PHP:

class SomeObj
{
    //This is the constructor for SomeObj
    function __construct() //two underscores
    {
    }
}

Many in PHP prefer the latter style of declaration because it allows for more uniformity. Either is accepted, and there's no penalty in using either, as a call to parent::__construct will call the class name constructor as well. It's really a matter of personal preference, and in NOLOH's case we chose the traditional way.

Now that you know how to declare a constructor, lets go over how they're used in NOLOH.

In NOLOH, all classes have constructors that enable you to set the most common properties of the object immediately, rather than having to wait until the object is instantiated. The constructor parameters always have defaults, so you can skip setting them at this point if you're ok with the defaults, or prefer to set these properties later. Furthermore, the order of the parameters was painstakingly chosen to allow for objects to be instantiated quickly, easily, and intuitively.

Lets look at the following examples.

function SomeFunc()
{
    //Let's examine the following Label instantiation
    $label = new Label("Test", 10, 10, 100, 100);
    /*The first parameter is the text of the label, followed by the left, top, width,
    and height. In this case the Label's text is "Test", its left and top are 10,
    and width and height are 100. This was all done in one line, rather than having
    to set it in 5 lines, as is normally the case with other web application
    development platforms.*/
}

Alternatively, you could choose to only specify the first few parameters, and leave the rest to their defaults.

function SomeFunc()
{
    //Let's examine the following Label instantiation
    $label = new Label("Test", 10);
    /*The first parameter is the text of the label, followed by the left. In this case
    the Label's text is "Test", its left is 10. The top, width, and height are set to
    the default values for the class. Due to the nature of nulls in PHP, you can only
    leave out trailing parameters. So, if you wanted to set only the text and top of
    this label, you would have to fill out the left parameter. NOLOH has painstakingly
    ordered the constructors, so you're usually not left having to do this.*/
}

Now that you know how constructors work, let's go into extending an object, and calling its parent constructor.

//Our new class, CustomPanel inherits from and extends NOLOH's built-in Panel class
class CustomPanel extends Panel
{
    /*CustomPanel constructor, four parameters are set with new defaults for CustomPanel
    overriding the default values for these parameters.*/
    function CustomPanel($left=4, $top=4, $width=320, $height=240)
    {
        /* Calls CustomPanel's parent constructor, Panel, passing the new default
        parameters.*/
        parent::Panel($left, $top, $width, $height);
    }
}
smallarrow
explanation arrowtool tip
handlebgexpand left
Right Up Outer CornerRight Down Outer CornerLeft Up Outer CornerLeft Down Outer Cornersearch-bottomclassconstantpropertymethodarticle
controltabsearchbig