【问题标题】:PHP Assigning Concatenated Methods in a Class PropertyPHP 在类属性中分配串联方法
【发布时间】:2016-07-20 20:12:55
【问题描述】:

为什么我不能像这样在类属性中赋值?首先是我得到的错误:

解析错误:语法错误,第 8 行 C:\wamp\www\site\people.php 中的意外 '$this' (T_VARIABLE)

代码:

<?php

$person = new People;
echo $person->getPersonInfo();

class People
{
private $person_info = $this->getName . ' ' . $this->getGender . ' and ' . $this->getAge() . '.';
private $person = array(
'name' => 'Sarah',
'gender' => 'female',
'age' => 21
);

public function __construct()
{
    echo 'Class is created.' . '<br>';
}

public function getName()
{
    return $this->person['name'];
}

public function getGender()
{
    return $this->person['gender'];
}

public function getAge()
{
    return $this->person['age'];
}

public function getPersonInfo()
{
    return $this->person_info;
}
}

【问题讨论】:

  • 因为php.net/manual/en/language.oop5.properties.php 这个声明可能包含一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时被求值,并且不能依赖于运行时信息以便进行评估。 在构造函数中构建它。

标签: php concatenation


【解决方案1】:

您不能像那样在类属性中设置动态值;它们必须是常数值。您必须在构造函数中进行此类分配

// NO
private $person_info = $this->getName . ' ' . $this->getGender . ' and ' . $this->getAge() . '.';

// YES
public function __construct() {
  $this->person_info = $this->getName . ' ' . $this->getGender . ' and ' . $this->getAge() . '.';
}

但你真正的意思是这个

// you're calling get methods, not properties
// make sure to use $this->getName(), not $this->getName
public function __construct() {
  $this->person_info = $this->getName() . ' ' . $this->getGender() . ' and ' . $this->getAge() . '.';
}

但实际上你所有的代码都是坏的

// This makes no sense to have as a constant property in a class
private $person = array(
  'name' => 'Sarah',
  'gender' => 'female',
  'age' => 21
);

这个类对你的目的更有意义

class Person { 

  // container for your info object
  private $info = [];

  // make info a parameter for your constructor
  public function __construct(array $info = []) {
    $this->info = $info;
  }

  // use PHP magic method __get to fetch values from info array
  public function __get(string $attr) {
    return array_key_exists($attr, $this->info)
      ? $this->info[$attr]
      : null;
  }

  // use PHP magic method __set to set values in the info array
  public function __set(string $attr, $value) {
    $this->info[$attr] = $value;
  }

  // single method to fetch the entire info object
  public function getInfo() {
    return $this->info;
  }
}

简单。让我们看看它现在是如何工作的

$p = new Person([
  'name' => 'Sarah',
  'gender' => 'female',
  'age' => 21
]);

echo $p->name, PHP_EOL;
// Sarah

echo $p->gender, PHP_EOL;
// female

echo $p->age, PHP_EOL;
// 21

echo json_encode($p->getInfo()), PHP_EOL;
// {"name":"Sarah","gender":"female","age":21}

因为$info数组现在是一个参数,你可以很容易地用其他数据创建Person对象

$q = new Person([
  'name' => 'Joey',
  'gender' => 'male',
  'age' => 15
]);

echo $q->name, PHP_EOL;
// Joey

echo $q->gender, PHP_EOL;
// male

echo $q->age, PHP_EOL;
// 15

echo json_encode($q->getInfo()), PHP_EOL;
// {"name":"Joey","gender":"male","age":15}

【讨论】:

  • 我真的很感激所有的新知识。现在还有很多东西要学,谢谢。回到主题,在评论中它说,“这个声明可能包括一个初始化,但是这个初始化必须是一个常量值——也就是说,它必须能够在编译时被评估,并且不能依赖于运行时信息以便进行评估。”现在对我来说英语太多了。
猜你喜欢
  • 2019-12-03
  • 2021-06-17
  • 2014-08-21
  • 2011-11-26
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 2015-03-08
  • 2022-08-05
相关资源
最近更新 更多