我经常喜欢上课来处理整个任务。用一些参数调用类,然后得到结果。
我不能这样做:
<?php
class MyClass{
function __construct() {
$this->one = $this->one();
$this->two = $this->two();
$this->three = $this->three();
return $this->three;
}
function one() {
$output = 'One';
return $output;
}
function two() {
$output = $this->one . 'Two';
return $output;
}
function three() {
$output = $this->two . 'Three';
return 'Three' . $this->two;
}
}
echo new MyClass();
我可以这样做:
<?php
class MyClass{
function run() {
$this->one = $this->one();
$this->two = $this->two();
$this->three = $this->three();
return $this->three;
}
function one() {
$output = 'One';
return $output;
}
function two() {
$output = $this->one . 'Two';
return $output;
}
function three() {
$output = $this->two . 'Three';
return 'Three' . $this->two;
}
}
$obj = new MyClass();
echo $obj->run();
但上述真的是正确的方法吗?然后我认为不需要构造。
答案 0 :(得分:3)
使用课程的正确方法&#39;构造函数,就是让它设置好可以使用的类。在许多情况下,构造函数除了接受依赖项(例如数据库对象)之外并没有做太多事情,并保存这些以供以后使用。
(Ab)在你的第一个例子中使用构造函数会导致很多困难,主要是因为简单地创建一个对象除了创建所述对象之外还有其他副作用。
以这种方式,你的第二个例子更接近于面向对象的编程,即使你仍然没有真正利用任何使用类的东西给你带来的东西。事实上,作为纯粹的程序代码编写本来会更好。
虽然我不知道你的代码是做什么的,但是我试图通过使用类来利用功能给出一个例子:
/**
* We need a DB connection in case we need to get something from the database.
* This is called a dependency, and we save it in the object at object creation time.
*
* Nothing that causes the object itself to do "work" is supposed to be here, only things
* necessary for actually creating the object in a state where we can _start_ working with it.
*
* @param PDO $db
*/
public function __construct (PDO $db) {
$this->db = $db;
}
/**
* Sets the value for each step. Legal values for $step is between 1 and 3, inclusive.
* Normally we'd have many setters, one for each property we want to change from outside.
* That can be anything from address to price and anything else one can think of.
*
* @param int $step
* @param int|string $value
*
* @throws InvalidArgumentException
* @return void
*/
public function set_value ($step, $value) {
if ($step <= 0 || $step > 3) {
throw new InvalidArgumentException("Step must be between 1 and 3, inclusive.");
}
$this->value[$step] = $value;
}
/**
* This is where the actual processing is done.
* In a normal class there would be several such functions
* each doing one specific thing (creating a new record, saving
* it to the database, validating stuff, etc).
*
* @return void
*/
public function do_processing () {
$this->result = implode(", ", $this->data);
}
/**
* Fetches the result of the class. Normally we have many getters, which return
* one part of the data associated with the object. Such as username, hash, email, etc.
*
* These are often in a 1-1 relationship with setters.
*
* @return string
*/
public function get_result () {
// If we have no calculated result, fetch it from the DB instead.
if (empty($this->result)) {
return $this->db->get_cached_result ();
}
// Returns the newly calculated result.
// Should probably also cache it, to make the above line useful.
return $this->result;
}
}
// A DB interface class, which extends PDO.
$db = new MyClassDB ();
$obj = new MyClass ($db);
$obj->set_value (2, "Two");
$obj->set_value (1, "One");
$obj->set_value (3, "Three");
$obj->do_processing();
echo $obj->get_result();
请注意,这是一个非常简单的课程,并没有为您提供如何正确利用课程的非常好的图片。我建议您查看更加丰富的课程,您可以在任何主要框架中找到它。
答案 1 :(得分:0)
您可以将您的类方法定义为静态,然后将它们称为&#34;静态&#34;正如PHP喜欢说的那样。像这样,
<?php
class MyClass {
private static $one;
private static $two;
private static $three;
public static function run() {
self::$one = self::one();
self::$two = self::two();
self::$three = self::three();
echo self::$three;
}
private static function one() {
return 'One';
}
private static function two() {
return self::$one . 'Two';
}
private static function three() {
return self::$two . 'Three';
}
}
MyClass::run();
?>
答案 2 :(得分:-1)
不建议使用构造函数的返回值。
class MyClass{
function __construct() {
$this->one = $this->one();
$this->two = $this->two();
$this->three = $this->three();
echo $this->three;
}
function one() {
$output = 'One';
return $output;
}
function two() {
$output = $this->one . 'Two';
return $output;
}
function three() {
$output = $this->two . 'Three';
return 'Three' . $this->two;
}
}
new MyClass();
答案 3 :(得分:-1)
<?php
class MyClass{
private $one; //private or public or protected.
private $two; //placeholder class variables.
private $three;
function __construct() {
$this->run();
}
function run(){
$this->one(); //set value one
$this->two(); //set value two
print $this->three(); //set and then output value 3.
}
function one() {
$output = 'One';
return $output;
}
function two() {
$output = $this->one . 'Two';
return $output;
}
function three() {
$output = $this->two . 'Three';
return 'Three' . $this->two;
}
}
//printed output from run automatically given to output buffer.
// can't print 'new class' syntax.
new MyClass(); //outputs result of function three();
返工类的作用是变量已经被移动为类变量而不是__construct
中的所有变量,然后在最终之前使用相关方法设置这些变量。一个按原件退回。