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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
haydenp
Forum Newbie
Posts: 1
Joined: Wed Jan 28, 2009 11:01 am

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

Post 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);
 
?>
 
Last edited by haydenp on Wed Jan 28, 2009 12:21 pm, edited 1 time in total.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

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

Post by andyhoneycutt »

this sounds like a homework question =]
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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
Post Reply