doubingjian2006 2017-03-28 08:00
浏览 26

php变量对象属性

this maybe seems like duplicate, but I tried all answers and none of them helped me. I have a:

class Api extends Controller {
    private $host;
    function __construct() {
        parent::__construct();
    }
}

I don't have problem with this part, because many functions inside Api class are working

my problem is with below:

function POS() {
    $host = defined('url_parameter_1') ? url_parameter_1 : null;
    $operation = defined('url_parameter_2') ? url_parameter_2 : null;
    $transaction_id = defined('url_parameter_3') ? url_parameter_3 : null;

    if ($host != null) { 
        $this->{$host};
    }
    else { 

    }

}

in this example url_parameter_1 = PashaBank and I have a function:

private function PashaBank() {
    $data = array();
    $params['logo'] = '/public/img/logo-pashabank.png';
    $params['description'] = 'Some description text';

    echo $this->build_html($params);
}

i get an error

Notice: Undefined property: Api::$$host in /var/www/vhosts/

when i use $this->{'$host'};

and error

Undefined property: Api::$PashaBank in /var/www/vhosts/ when i use $this->{$host};

Class fully looks like this:

class Api extends Controller {
    private $host;
    function __construct() {
        parent::__construct();
    }
    function POS() { 
        $host = defined('url_parameter_1') ? url_parameter_1 : null;
        $operation = defined('url_parameter_2') ? url_parameter_2 : null;
        $transaction_id = defined('url_parameter_3') ? url_parameter_3 : null;

        if ($host != null) { 
            $this->{$host};
        }
        else { 

        }   
    }

    function build_html($params) {
        $html = '<div class="pos_container">';
        $html .= '<div class="pos_header"><img src="'.$params['logo'].'" /></div>';
        $html .= '<div class="pos_body">';
        $html .= '<p class="pos_description">'.$params['description'].'</p>';

        $html .= '</div>'; // pos_body
        $html .= '</div>'; // pos_container

        return $html;
    }
    private function PashaBank() { 
        $data = array();
        $params['logo'] = '/public/img/logo-pashabank.png';
        $params['description'] = 'Some description text';

        echo $this->build_html($params);
    }   
}

please help, i'm stuck in here

  • 写回答

2条回答 默认 最新

  • duanjiao7440 2017-03-28 08:14
    关注

    First of all, your class doesn't have field named "PashaBank". It's the reason of notice you can see:

    Notice: Undefined property: Api::$$host in /var/www/vhosts/

    By the way, there is no reason to use statement:

    $this->{$host}

    instead of:

    $this->$host

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False