3 Replies - 1504 Views - Last Post: 18 November 2012 - 10:39 PM Rate Topic: -----

#1 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Newbie help with implementation of __call() method

Posted 16 March 2012 - 12:19 AM

Hey all,

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

?>




Is This A Good Question/Topic? 0
  • +

Replies To: Newbie help with implementation of __call() method

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3476
  • View blog
  • Posts: 10,009
  • Joined: 08-June 10

Re: Newbie help with implementation of __call() method

Posted 16 March 2012 - 12:45 AM

problem with 3rd line in your output:
you don't have access to the properties of the Address object. i.e. in __call() you only code for properties of the Student object.

problem with 5th line of your output:
since the Address properties are not available, PHP creates them on the fly in the Student object when you call the setters.

solution: make your address object available so you can do
$student->address->street
// or
$student->getAddress()->getStreet()

this way you don't even need to use __call() (which code I'd deem nearly unreadable/non-understandable).

note: if you don't do any input verification, you could use public properties.


tips:
verify the input in the setters!

use meaningful names (variable name length is not a performance issue!)

you have an object for the address, use it.
class Student
{
    public function __construct($name, $age, Address $address) {
        $this->address = $address;
// ...



if you already have getters and setters, there is hardly any point in using magic methods (__get(), __set(), __call()) for that purpose (it helps you keeping your interface straight and nothing is more confusing than a method you don't find in the class' API)

magic methods eat performance

you might be interested in the Decorator Pattern and the Composite Pattern

This post has been edited by Dormilich: 16 March 2012 - 12:56 AM

Was This Post Helpful? 3
  • +
  • -

#3 rvbsanjose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 17-October 11

Re: Newbie help with implementation of __call() method

Posted 16 March 2012 - 12:15 PM

Dormilich -- Much appreciated for the great explanation! If I'm able to make my career change into software development, I think I'm gonna owe this great community for all the help along the way. Got it working as soon as I read the post. :tup:
Was This Post Helpful? 0
  • +
  • -

#4 okcher  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-November 12

Re: Newbie help with implementation of __call() method

Posted 18 November 2012 - 10:39 PM

View Postrvbsanjose, on 16 March 2012 - 12:15 PM, said:

Dormilich -- Much appreciated for the great explanation! If I'm able to make my career change into software development, I think I'm gonna owe this great community for all the help along the way. Got it working as soon as I read the post. :tup:/>


Hi rvsanjose,

May I see the final answer of this code? I'm doing pretty much the same and got confused with that __call method.

Thank you!

P.S. I hope you will success in your carreer :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1