douliang1369 2016-03-09 00:24
浏览 23
已采纳

PHP类属性未传入方法

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?

  • 写回答

1条回答 默认 最新

  • doufu6504 2016-03-09 00:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度