Shawn:

  I beleive I can clarify the purpose of objects/classes for you and 
enrich my clarification with a couple of examples.


THE DIFFERENCE BETWEEN "OBJECT" AND "CLASS":

  A class defines the structure of an object. A class is a "template" 
for an object. An object is a container for data and for functions that 
operate on that data. A class is a roadmap -- it defines the object's 
structure. You can then create several objects using one class. These 
several objects are separate from one another, but identical in 
structure. Each can contain different data but use the same functions to 
operate on that data.

  Functions contained within an object class are called methods. 
Functionally, methods are exactly the same as functions except that they 
are restricted to the scope of the object. Generally, a method is a 
function that operates on the object's data. An object contains variable 
data including variables, arrays and objects. These variables are called 
the object's properties.


WHEN TO USE OBJECTS:

  Objects could be used when you have several similar pieces of data 
especially when you wish to have that data manipulated by the same 
functions but wish to maintain separation between these data objects.


EXAMPLE:

  Let's say, for example, that you wished to store information about a 
number of different people within a single script execution. An 
insurance site might wish to gather information about the primary 
applicant, his wife and their three children all on the same page.

  You and I would agree that all people have similar properties; we all 
have an age, a height, a weight - we might smoke etc. Therefore, a class 
for people could be defined.

        class Human
                {
                        var $age;
                        var $height_inches;
                        var $weight_pounds;
                        var $smoker;

                        function HeightWeightRatio()
                                {
                                        $ratio = $this->height_inches / 
$this->weight_pounds;
                                        return $ratio;
}                               }
}               }

  To create an instances of the class (an object) you would use 
something like this:

        $father = new Human;
        $mother = new Human;

  Once you have instantiated the class (previous lines) you could then 
set the values of the object with code like:

        $father->age = 49;
        $father->height = "5'11";
        $father->weight = 215;
        $father->smoker = false;

        $father_ratio = $father->HeightWeightRatio();

        $mother->age = 47;
        $mother->height = "5'5";
        $mother->weight = 145;
        $mother->smoker = true;

        $mother_ratio = $mother->HeightWeightRatio();

  By now, I'm sure you get the picture.


THE MOST USEFUL EXAMPLE FOR ME THUS FAR:

class HTMLTemplate
        {
        
                var $template;
                var $html;
                var $parameters 
= array();
                var $populated = false;
                
                function HTMLTemplate (
$template)
                        {
                
                                $this->template = $template;
                                $this->html = 
implode ("", (file($this->template)));
                
                        }
        
                function SetParameter (
$variable, $value)
                        {
                        
                                $this->parameters[$variable] = $value;
                        
                        }
        
                
function PopulateValues ()
                        {
                
                                foreach ($this->parameters as 
$key => $value)
                                        {
                                        
                                                $template_name = '<!--{' . $key . '}--
>';
                                                $this->html = str_replace 
($template_name, $value, $this->html);
                                        }
                                
                                
$this->populated = true;
                        }
        
                function ReturnHTML ()
                        {
                                if (
$this->populated)
                                        {
                                                return $this->html;
                                        }
                                else
                                        {
                                                
$this->PopulateValues();
                                                return $this->html;
                                        }
                        }
        }

  This class allows you to use an html template file which you can paste 
dynamic values/content into. Here's an example of how to use it:

<?php

        $title = "My Web Page";
        $content = "Hello, this is my web page.";

        $template = new HTMLTemplate("/mytemplate.html");
        $template->setParameter("PAGE-TITLE", $title);
        $template->setParameter("CONTENT", $content);
        
        echo $template->ReturnHTML(); // Sends html to the browser

        // It is important to destroy an object once you are finished
        // using it so that you can free up the memory space that it uses
        
        unset($template); // Destroys instance of object

?>

HTML TEMPLATE FILE:

  My html template file might look like this:

        <html>
                <head>
                        <title><!--{PAGE-TITLE}--></title>
                </head>
                <body>
                        <!--{CONTENT}-->
                </body>
        </html>


Christopher Raymond,
Oasis Networks <-- Web Development & Macintosh Systems Consulting

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to