I'm not taking credit for some of the solution, I did have help along the way, but it's still not where it needs to be. With only 3 months of programming experience, I am struggling with this assignment. I am not looking for someone to complete it for me but rather someone who can help me with some ideas or pseudo code to help me along the right path.
Fill in the code in student-code.php
function __call($method, $arguments) {
// Need more code
}
so that the properties of street, city, state can be retrieved and assigned using the student instance. The following is the output without any code modification
John Smith
50
, ,
The address has been updated:
, ,
The following is the output that we want after the code gets filled in.
John Smith
50
100 main street, Sunnyvale, CA
The address has been updated:
50 second street, Palo Alto, CA
And this is the current output
John Smith
50
, ,
The address has been updated:
50 second street, Palo Alto,
Student Object ( [name:Student:private] => john smith [age:Student:private] => 50 [address:Student:private] => Address Object ( [street:Address:private] => 100 main street [city:Address:private] => Sunnyvale [state:Address:private] => CA ) [street] => 50 second street [city] => Palo Alto )
<?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) {
$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 "<br />";
echo $s->age;
echo "<br />";
echo $s->street . ", " . $s->city . ", " . $s->state;
echo "<br />";
$s->street = "50 second street";
$s->city = "Palo Alto";
echo "The address has been updated:<br />";
echo $s->street . ", " . $s->city . ", " . $s->state;
echo '<br /><br />';
print_r($s);
?>

New Topic/Question
Reply



MultiQuote




|