douyi4544 2016-10-09 01:03
浏览 69

传递类时没有传递方法? PHP

What I want to ask is,

let's say I have a class Info and there's a getter in the info called getABC()

In a controller I assigned something like

$info = new Info();

$variable['info'] =$info;

and $variable is being passed into the view.

In the view, am I able to use something like $variable['info']->getABC() ? I know I can just test it out myself, and failed saying something like it did not exist and $variable['info'] does show something though.

I just want to make sure that $variable['info']->getABC() is suppose to NOT work or it should but I am just doing something wrong that's why I couldn't get what's needed.

actual code below........

Class

class CreditCardPayment{

private $_card_type = '';
private $_card_number = '';
private $_card_number_last_4 = '';

public function setCardType($v)
{
    $this->_card_type = $v;
    return $this;
}

public function setCardNumber($v)
{
    $this->_card_number = $v;
    return $this;
}

public function setCardNumberLast4($v) {
    $lastFourDigits =  substr($v, -4);
    $output = 'xxxx-xxxx-xxxx-' . $lastFourDigits;
    $this->_card_number_last_4 = $output;
    return $this;
}

public function getCardType() {
    return $this->_card_type;
}

public function getCardNumber() {
    return $this->_card_number;
}

public function getCardNumberLast4() {
    return $this->_card_number_last_4;
}

}

and in Controller let's say when it's successful....it'll be something like this where $creditCardPayment = new CreditCardPayment and I tried var_dump($creditCardPayment) that definitely info is all filled and of course those are private variables so I had to use the getter retrieve them.

Controller

$ordermess['creditCardPaymentInfo'] = $creditCardPayment;
 \Yii::$app->session->set('ordermess', $ordermess);
 $this->redirect('/pay/completed');

then in my view...I did this as testing

<?php 
    echo '<pre>'; 
    echo ($ordermess['creditCardPaymentInfo']->getCardNumberLast4()); 
    echo '</pre>'; 
    die; 

?>

then when I load the page I would get error.

Call to a member function getCardNumberLast4() on a non-object

  • 写回答

1条回答 默认 最新

  • drfkl66684 2016-10-09 01:09
    关注

    Yes. It is supposed to work. If there is sanitization code in the function assigning variables to the view, it could be converting the object to an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用