[英]how can we assign input type in to an variable
Here I am having an doubt,i know it doesn't look relevant to you people but i need to get it. 在这里,我有一个疑问,我知道它看起来与你们没有关系,但我需要得到它。 The thing is can we assign an input type to an variable like this. 关键是我们可以将输入类型分配给这样的变量。
<?php $option = '<input type="text" name="project_id" id="pr_id"/>';
var_dump($option);?>
Here pr_id
contains an value and a thinking to assign it to an variable pr_id
包含一个值和将其分配给变量的想法
Sure, you can assign anything! 当然,您可以分配任何东西! Here's an Input object I just created, have a look at this: 这是我刚刚创建的Input对象,请看一下:
<?php
class Input
{
/** @var array $attributes */
private $attributes = [];
/**
* @param $key
* @return mixed|string
*/
public function getAttribute($key)
{
return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
}
/**
* @param $key
* @param $value
* @return $this
*/
public function setAttribute($key, $value)
{
$this->attributes[$key] = $value;
return $this;
}
/**
* @param array $attributes
* @return $this
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
public function render()
{
$html = '<input ';
foreach ($this->getAttributes() as $key => $val) {
$html .= $key.'="'.$val.'" ';
}
$html .= '/>';
return $html;
}
}
So you can now generate an Input with the following code: 因此,您现在可以使用以下代码生成输入:
$input = new Input();
$input->setAttribute('id', 'pr_id')
->setAttribute('name', 'project_id')
->setAttribute('type', 'text');
echo $input->render();
Which outputs: 哪个输出:
<input id="pr_id" name="project_id" type="text" />
Have a play with it here: https://3v4l.org/sAiWd 在这里玩一下: https : //3v4l.org/sAiWd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.