yii2 html input,yii2 hidden input value

Hello World!

You see, the main question while using hidden input is what kind of data you want to pass?

I will assume that you are trying to pass the user ID.

Which is not a really good idea to pass it here because

please check this link, and you will see that it's impossible to hide value attribute from users to see.

so what to do then?

See, this is the core of OOP in PHP.

and I quote from Matt Zandstr in his great book PHP Objects, Patterns, and Practice fifth edition

I am still stuck with a great deal of unwanted flexibility, though. I rely on the client coder to change a ShopProduct object’s properties from their default values. This is problematic in two ways. First, it takes five lines to properly initialize a ShopProduct object, and no coder will thank you for that. Second, I have no way of ensuring that any of the properties are set when a ShopProduct object is initialized. What I need is a method that is called automatically when an object is instantiated from a class.

Please check this example of using __construct() method which is mentioned in his book too.

class ShopProduct {

public $title;

public $producerMainName;

public $producerFirstName;

public $price = 0;

public function __construct($title,$firstName,$mainName,$price) {

$this->title = $title;

$this->producerFirstName = $firstName;

$this->producerMainName = $mainName;

$this->price = $price;

}

}

And you can simply do this magic.

$product1 = new ShopProduct("My Antonia","Willa","Cather",5.99 );

print "author: {$product1->getProducer()}\n";

This produces the following:

author: Willa Cather

In your case it will be something semilar to this, every time you create an object just pass the user ID to the user_id property, and save yourself a lot of coding.

Class Car {

private $user_id;

//.. your properties

public function __construct($title,$firstName,$mainName,$price){

$this->user_id = \Yii::$app->user->id;

//..Your magic

}

}

Good luck! And Happy Coding!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值