Php 方法重载:函数调用($Method,$arg)

Php 方法重载:函数调用($Method,$arg),php,Php,我有两个班:地址和学生。我需要对调用函数进行编码,以便使用student实例检索和分配街道、城市和州的属性 这是我对调用进行编码的输出,但它仅适用于到目前为止的最后一行输出: John Smith 50 , , The address has been updated: 50 second street, Palo Alto, CA 输出的第三行应为: 100 main street, Sunnyvale, CA 这就是我得到stack的地方 这是我的密码。我将感谢任何帮助 <?php

我有两个班:地址和学生。我需要对调用函数进行编码,以便使用student实例检索和分配街道、城市和州的属性

这是我对调用进行编码的输出,但它仅适用于到目前为止的最后一行输出:

John Smith
50
, ,
The address has been updated:
50 second street, Palo Alto, CA
输出的第三行应为:

100 main street, Sunnyvale, CA
这就是我得到stack的地方

这是我的密码。我将感谢任何帮助

<?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, Address $address) {
    $this->name = $n;
    $this->age = $a;
    $this->address = $address;
}

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) {
    // Need more code 

    $mode = substr($method,0,3);
    $var = strtolower(substr($method,3));
    if ($mode =='get'){
        if (isset($this -> $var)){
            return $this ->$var;
        }
    } elseif ($mode == 'set') {
        $this ->$var = $arguments[0];
        }
    } 

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


//print_r($s);

?>
街道、城市和州必须是公共的,或者使用getter

更改下一步:

$s = new Student('john smith', 50, '100 main street', 'Sunnyvale', 'CA');
echo $s->address->street . ", " . $s->address->city . ", " . $s->address->state;
下一步:

$s = new Student('john smith', 50, new Address('100 main street', 'Sunnyvale', 'CA'));
echo $s->address->getStreet() . ", " . $s->address->getCity() . ", " . $s->address->getState();
下一个改变是:

$s = new Student('john smith', 50, '100 main street', 'Sunnyvale', 'CA');
echo $s->address->street . ", " . $s->address->city . ", " . $s->address->state;
下一步:

$s = new Student('john smith', 50, new Address('100 main street', 'Sunnyvale', 'CA'));
echo $s->address->getStreet() . ", " . $s->address->getCity() . ", " . $s->address->getState();
学生构造函数需要一个Address对象,属性street、city和state是私有的,需要使用getter


)-

不值得。要么给学生添加一个get/setAddress,然后在那里调用getter/setter,要么给学生添加代理方法。非常感谢!这是可行的,但是我有没有办法改变函数调用中的某些内容,而不是改变常规代码?很抱歉这么问,我是PHP新手,所以试着学习:是的,另一种方法是将private更改为public。更改下一个:{private$name;private$age;private$address;}下一个:{public$name;public$age;public$address;}