方法重载 - 函数__call($ method,$ arguments)PHP

时间:2013-03-01 06:28:09

标签: php overloading

这是我第一次使用Stack ...而且我是PHP新手......但这是我的问题:

<?php
class Address {
    private $street;
    private $city;
    private $state;

    function __construct($s, $c, $st) {
        $this->street = $s;
        $this->city = $c;
        $this->state = $st;
    }
    function setCity($c) {
        $this->city = $c;
    }
    function getCity() {
        return $this->city;
    }
    function setState($s) {
        $this->state = $s;
    }
    function getState() {
        return $this->state;
    }
    function setStreet($s) {
        $this->street = $s;
    }
    function getStreet() {
        return $this->street;
    }
}
class Student {
    private $name;
    private $age;
    private $address;

    function __construct($n, $a, $s, $c, $st) {
        $this->name = $n;
        $this->age = $a;
        $this->address = new Address($s, $c, $st);
    }

    function getName() {
        return ucwords($this->name);
    }

    function getAge() {
        return $this->age;
    }

    function setName($n) {
        $this->name = $n;
    }

    function setAge($a) {
        $this->age = $a;
    }

    function __set($name, $value) {
        $set = "set".ucfirst($name);
        $this->$set($value);
    }

    function __get($name) {
        $get = "get".ucfirst($name);
        return $this->$get();
    }


    function __call($method, $arguments) {
        // JUST NEED CODE HERE TO SOLVE FOR THE PROBLEM
    }


}
$s = new Student('john smith', 50, '100 main street', 'Sunnyvale', 'CA');
echo $s->name;
echo "\n";
echo $s->age;
echo "\n";
echo $s->street . ", " . $s->city . ", " . $s->state;
echo "\n";
$s->street = "50 second street";
$s->city = "Palo Alto";
echo "The address has been updated:\n";
echo $s->street . ", " . $s->city . ", " . $s->state;


$s = new Student("John", 30, new Address("San Jose", "CA"));
$t = clone $s;
$t->name = "Jack";
$t->address->city = "Santa Clara";
echo $s;
echo "\n";
echo $t;
?>

我正在回答这个问题,但我只能在函数__call

中添加代码
  约翰史密斯   50个
  100主要街道,Sunnyvale,加州
  地址已更新:
  50街道,加利福尼亚州帕洛阿尔托

希望有人可以提供帮助!

0 个答案:

没有答案