Page 1 of 1

OOP - When and Why would you use Example 1 over Example 2

Posted: Wed Jan 28, 2009 11:11 am
by haydenp
Hi

In the two short examples below - when would you use Example 1 over Example 2 for passing values to a method within a class? Are there any "best practices" when deciding this? Is this more "up to the individual" or is there a REAL reason/situation when you would opt to use Example 1 over Example 2 or vis versa?

Code: Select all

 
<?php
 
/////////// EXAMPLE 1 ///////////
 
class ExampleClassA
{
    public $data = NULL; 
    function MyFunction()
    {
        return $this->data + 100;
    }    
}
 
// Calling Script
 
$ExampleA = new ExampleClassA;
$ExampleA->data = 100;
echo "<p>A = " . $ExampleA->MyFunction();
 
/////////// EXAMPLE 2 ///////////
 
class ExampleClassB
{ 
    function MyFunction($data)
    {
        return $data + 100;
    }    
}
 
// Calling Script
 
$ExampleB = new ExampleClassB;
echo "<p>B = " . $ExampleB->MyFunction(100);
 
?>
 

Re: OOP - When and Why would you use Example 1 over Example 2

Posted: Wed Jan 28, 2009 11:13 am
by andyhoneycutt
this sounds like a homework question =]

Re: OOP - When and Why would you use Example 1 over Example 2

Posted: Wed Jan 28, 2009 11:42 am
by Mark Baker
Try runing the example code -- perhaps echoing $Example->MyFunction and $Example->MyFunction(100) a few times -- to see what differences there are between the two