PHP构造函数引用变量使其他变量不起作用

时间:2015-04-20 01:12:59

标签: php constructor

我有一个构造函数。除了最后一部分,一切都有效。

class Article {
  public $category;
  public $title;
  public $text;
  public $intro;


  public function __construct($category, $title, $text) {
      $this->category = $category;
      $this->title = $title;    
      $this->text = $text;
      $this->intro = substr($this->text, 0, 40);
  }
}

没有创建简介。

1 个答案:

答案 0 :(得分:1)

它运作得很好:

<?php

class Article {
  public $category;
  public $title;
  public $text;
  public $intro;


  public function __construct($category, $title, $text) {
      $this->category = $category;
      $this->title = $title;    
      $this->text = $text;
      $this->intro = substr($this->text, 0, 40);
  }
}

$test=new Article('a','b','A question is a linguistic expression used to make a request for information, or the request made using such an expression. The information requested may be provided in the form of an answer.');

echo $test->intro; //A question is a linguistic expression us

演示:http://codepad.org/3aXaWZHY