Reputation: 301
To start off, I am extremely new to PHP and am trying to understand how to work with an object class.
For several days now I've had a challenge that I haven't been able to solve. The problem is that a property of a class isn't being called/utilized inside a method of the class. I know the property isn't empty because I threw in a test method to confirm it.
There must be something I'm missing, and I hope it isn't painfully obvious, as I've spent several days trying different solutions to no avail.
Below is my code with comments:
<?php
/************* global variables ******************************/
$company_name = "Stay Cool HVAC";
$street = '12345 Rockwell Canyon Rd.';
$company_citystatezip = "Hometown, CA 91777";
$company_address = "<center>$company_name <br/> ". "<center>$street <br />". "<center>$company_citystatezip";
/************* end global variables **************************/
echo '<H1 align="center">PHP Class Example</H1>';
class Company {
//// insert object variables (properties)
var $address;
//// insert methods below here
//// Test to see that address property is set to $company_address variable
function __get($address){
return $this->address;
}
function getHeader($company_name, $color) {
$topheader = "<TABLE align='center'; style='background-color:$color;width:50%'><TR><TD>";
$topheader .= "<H1 style='text-align:center'>$company_name</H1>";
$topheader .= "</TD></TR></TABLE>";
return $topheader;
}
//// The address property isn't passing to output in method
function getFooter($color) {
$this->address;
$bottomfooter = "<TABLE align='center'; style='background-color:$color;width:50%'><TR><TD>";
$bottomfooter .= "<center><b><u>$address</center></b></u>";
$bottomfooter .= "</TD></TR></TABLE>";
return $bottomfooter;
}
}
$companybanner = new Company();
echo $companybanner->getHeader($company_name, gold);
echo "<br/>";
$companybanner->address = "$company_address";
echo $companybanner->getFooter(blue);
// Test to confirm that "address" property is set - working
echo "<br />";
echo $companybanner->getaddress;
?>
Hopefully you can see that the "address" property is suppose to be outputted inside a blue colored table from the "getFooter" method. Instead, my result is a blue line with no text. Also, the "address" property isn't null as I did include a test with the "__get($address)" method.
Any ideas what I'm doing wrong?
Upvotes: 1
Views: 184
Reputation: 852
Maybe you should replace
function getFooter($color) {
$this->address;
With
function getFooter($color) {
$address = $this->address;
?
The way I understaind behavior of php, this line
$bottomfooter .= "<center><b><u>$address</center></b></u>";
would try to use local variable (local to the function) $address but $address is not defined. As I understand how php works - this line
$this->address;
would be interpreted in this manner: if address = "abc" is the same as telling interpreter to do
"abc";
No operations specified.
But I suppose $address = $this->address; is not the only way to solve your problem. I think you could use this just fine:
$bottomfooter .= "<center><b><u>{$this->address}</center></b></u>";
Hope that helps.
Upvotes: 1