PHP中的语法和构造函数
当从SQL数据库实例化一个对象时,我读到我应该使用PHP中的语法和构造函数,php,constructor,Php,Constructor,当从SQL数据库实例化一个对象时,我读到我应该使用hydrate()函数来填充我的对象,而不是直接使用构造函数 以下代码之间是否存在差异 含水合物(): class User { // attributes ... public function __construct(array $data = array()) { if (!empty($data)) { $this->hydrate($data);
hydrate()
函数来填充我的对象,而不是直接使用构造函数
以下代码之间是否存在差异
含水合物():
class User {
// attributes ...
public function __construct(array $data = array()) {
if (!empty($data)) {
$this->hydrate($data);
}
}
public function hydrate(array $data) {
foreach ($data as $key => $value) {
// One gets the setter's name matching the attribute.
$method = 'set'.ucfirst($key);
// If the matching setter exists
if (method_exists($this, $method)) {
// One calls the setter.
$this->$method($value);
}
}
}
// Getters/Setters and methods ...
}
class User {
// attributes ...
public function __construct(array $data = array()) {
if (!empty($data)) {
foreach ($data as $key => $value) {
// One gets the setter's name matching the attribute.
$method = 'set'.ucfirst($key);
// If the matching setter exists
if (method_exists($this, $method)) {
// One calls the setter.
$this->$method($value);
}
}
}
}
// Getters/Setters and methods ...
}
直接进入构造函数:
class User {
// attributes ...
public function __construct(array $data = array()) {
if (!empty($data)) {
$this->hydrate($data);
}
}
public function hydrate(array $data) {
foreach ($data as $key => $value) {
// One gets the setter's name matching the attribute.
$method = 'set'.ucfirst($key);
// If the matching setter exists
if (method_exists($this, $method)) {
// One calls the setter.
$this->$method($value);
}
}
}
// Getters/Setters and methods ...
}
class User {
// attributes ...
public function __construct(array $data = array()) {
if (!empty($data)) {
foreach ($data as $key => $value) {
// One gets the setter's name matching the attribute.
$method = 'set'.ucfirst($key);
// If the matching setter exists
if (method_exists($this, $method)) {
// One calls the setter.
$this->$method($value);
}
}
}
}
// Getters/Setters and methods ...
}
当您有许多属性的类,每个属性都有自己的setter和特定的检查时,这是一种很有用的方法来调用它们,而不是逐个调用 它的第二个用途是,如果您需要使用新值重用对象(比如说执行测试)。您不必重新构建一个新的设置器(或每个设置器),只需重新构建,您的类属性就会更新 以下是一个基本示例:
<?php
class Test
{
protected $titre;
protected $date;
protected ...
// And so on
public function __construct($value = array())
{
if(!empty($value))
$this->hydrate($value);
}
public function hydrate($data)
{
foreach ($data as $attribut => $value) {
$method = 'set'.str_replace(' ', '', ucwords(str_replace('_', ' ', $attribut)));
if (is_callable(array($this, $method))) {
$this->$method($value);
}
}
}
public function setTitle($title)
{
// Do specific check
$this->title = $title;
}
public function setDate($date)
{
// Do specific check
$this->date = $date;
}
}
$test = new Test(array("title" => "helloworld", ...));
// Manipulate the $test var and change stuff
...
$new_values = array("title" => "Hello, I am back", ...);
$test->hydrate($new_values);
?>
非常基于观点,但它可以保持构造函数干净,并允许您抽象到抽象类或特征。如果您有一个函数,那么您也可以在构建对象后调用它,以防需要。如果你想在不创建一堆对象的情况下用不同的输入测试特定的功能,这是很有用的。是的,我知道这一点,但是我想知道为什么我不应该把代码直接放到构造函数中?为了整洁起见?假设您有许多属性,并且必须对它们执行自定义检查,那么您的构造函数将充满无意义,稍后,如果您需要更改这些值,则无法重新检查它们。通过使用水合物功能,您可以保持一切干净整洁,您可以使用新值再次调用水合物功能,以填充字段。如果有助于显示我的问题,请更新我的问题ask@Will我编辑了我的答案,以便更好地帮助您理解检查空的意义是什么?如果数组为空,foreach
仍将立即退出。。。这只是为了避免方法调用而进行的微观优化吗?