PHP输出类的结果

I often like to have classes take care of a whole task. Call the class with some arguments and then get the result.

I can't do this:

<?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();

I can do this:

<?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();

But is the above really the correct way to do it? Then I don't see the need for a construct.

The proper way to use a class' constructor, is to have it set up the class ready to be used. In many instances the constructor doesn't do much beside accept the dependencies (database object, for instance), and saves these for later use.
(Ab)using the constructor as you've done in your first example leads to many difficulties along the line, mostly due to the fact that simply creating an object has additional side-effects beside just creating said object.

In that manner your second example is a lot closer to what object-oriented programming really is, even though you're still not really taking advantage of anything that using a class gives you. In fact, it would have been better off written as purely procedural code.

While I do not know what your code does, I've attempted to make an example taking advantage of features using a class gives you:

    /**
     * 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();

Please note that this is a pretty trivial class, and doesn't give you a very good picture of how to leverage classes properly. I recommend looking at a more fleshed out class, which you can find in any of the major frameworks.

You could define your class methods as static, then call them "statically" as PHP likes to say. Like so,

<?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();

?>

not recommended to use the constructor's return value.

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();
<?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();

What the reworked class does is that the variables have been moved to being class variables rather than all set within the __construct, then these are each set with the associated methods before the final one being returned as per the original.