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);
?>