I have this code which work …
class vm_ps_product extends vmAbstractObject {
var $myVar = '324';
function validate() {
$localTaxArray = array(
"hendersen" => "0.10",
"patrick" => "0.09"
);
if ( array_key_exists($city, $localTaxArray) ) {
return true;
} else {
return false;
}
}
}
I want to move localTaxArray outside the function like …
class vm_ps_product extends vmAbstractObject {
var $myVar = '324';
var $localTaxArray = array(
"hendersen" => "0.10",
"patrick" => "0.09"
);
function validate() {
if ( array_key_exists($city, $localTaxArray) ) {
return true;
} else {
return false;
}
}
}
But the code no longer works as before. What I did wrong?
Neither of those objects have ever worked.
Can you elaborate on how you would like these to behave?
Sorry, let me post some working sample code.
Here is code that works …
<?php
class Product {
function validate() {
$localTaxArray = array(
"hendersen" => "0.10",
"patrick" => "0.09"
);
$city = "hendersen";
if ( array_key_exists($city, $localTaxArray) ) {
echo("Tax on $city of " . $localTaxArray[$city] . "<br>");
} else {
echo("No tax");
}
}
}
$myProduct = new Product();
$myProduct->validate();
?>
It displays correctly “Tax on hendersen of 0.10”.
Now I want to refactor that code so that instead of $localTaxArray being defined inside the function, it is defined inside the class. Like this…
<?php
class Product {
$localTaxArray = array(
"hendersen" => "0.10",
"patrick" => "0.09"
);
function validate() {
$city = "hendersen";
if ( array_key_exists($city, $localTaxArray) ) {
echo("Tax on $city of " . $localTaxArray[$city] . "<br>");
} else {
echo("No tax");
}
}
}
$myProduct = new Product();
$myProduct->validate();
?>
But when I do that, the code no longer works. I also didn’t know how to get my sample code to display error messages.
CaraZ
November 5, 2010, 7:14pm
4
I haven’t worked with PHP classes lately, but this ought to work:
<?php
class Product {
public $localTaxArray = array(
"hendersen" => "0.10",
"patrick" => "0.09"
);
function validate() {
$city = "hendersen";
if ( array_key_exists($city, $this->localTaxArray) ) {
echo("Tax on $city of " . $this->localTaxArray[$city] . "<br>");
} else {
echo("No tax");
}
}
}
$myProduct = new Product();
$myProduct->validate();
?>
note $this->localTaxArray
Tried it. It no longer giving me an error. But it is not giving me the correct results as in the original code. It keeps saying “No tax” – almost as if there is no data stored in $localTaxArray.
Using $this->localTaxArray is giving me an error.
Using $this.localTaxArray is giving no error, but wrong results (as if no data in $localTaxArray).
Maybe you can *******ize this.
<?php
class Product
{
protected
$city = 'London';
public function getCity(){
return $this->city;
}
}
class Tax
{
protected
$cities = array(
'London' => 0.20
);
public function getTaxForProduct(Product $product){
if(array_key_exists($product->getCity(), $this->cities)){
return $this->cities[$product->getCity()];
}
return false;
}
}
$sheriff = new Tax;
$product = new Product;
printf(
'The tax rate for %s is %01.2f%%',
$product->getCity(),
$sheriff->getTaxForProduct($product)
);
/*
The tax rate for London is 0.20%
*/
CaraZ
November 5, 2010, 8:47pm
7
Ah. The code I posted works for me as you said you needed it to. Which error do you get?
Or, if $this.localTaxArray works, perhaps you could try printing it out to see what it contains, if anything.
Thanks CaraZ. You are right. Your code does work.
I got the $ in front of the variables wrong.
Funny there is $ in here…
public $localTaxArray
but not in front of localTaxArray as in here …
$this->localTaxArray
I guess I have to get used to these PHP syntax.
By the way, I finally got my PHP to display errors by putting the following at the top of PHP file.
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
That helps.
CaraZ
November 5, 2010, 9:20pm
9
artcoder:
Thanks CaraZ. You are right. Your code does work.
I got the $ in front of the variables wrong.
Funny there is $ in here…
public $localTaxArray
but not in front of localTaxArray as in here …
$this->localTaxArray
Well, technically, there is a $ in both, there’s just an extra this-> injected to reference a member of a class, as opposed to a ‘garden-variety’ variable.
In any case, glad the problem is solved