Problem with using func_num_args to emulate get/set?

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Problem with using func_num_args to emulate get/set?

    I'm using func_num_args to create a get/set type of function. Will this
    have unpredictable results, or or lead to some sort of problem I'm just not
    aware of? (I'm a bit of a newbie w/PHP.)

    Here's an example:

    class MyClass
    {
    var $name;
    function var_name($value)
    {
    if (func_num_args() == 0) // if there's no arg, get
    return $this->name;
    $this->name = $value; // else set
    }
    }

    $my_obj = new MyClass;
    $my_obj->var_name("Rob"); // set $my_obj->name to 'Rob'
    $name = $my_obj->var_name(); // now set $name to 'Rob'


    Using PHP 4.3.4, thanks!
    Bob Guest

  2. Similar Questions and Discussions

    1. How to emulate F5 (Refresh) for the currently editeddocument?
      Hi, I have a custom object which makes something like this (in the insertObject() function): - checks if a comment like <!--blah(.*)? --> exists...
    2. Best way to emulate number of requests cap
      Hi all: I want to write a very simple web service that just reads 1 XML file and serves it out to emulate the real web service. However I know...
    3. Emulate a explorer window
      How can i display a web page on top of a current web page without having to open a new internet explorer window. For example you have a calendar text...
    4. Emulate perls local
      Hello Ruby Fans, I'm working on the ruby section of the pleac project, (http://pleac.sourceforge.net) because I think a good way to learn a new...
    5. How can I emulate my browsers HTTP_USER_AGENT?
      Folks, I use Apache/PHP with Webazlier to view the log files. I get a number of user agents (web browsers?) that visit the website - For...
  3. #2

    Default Re: Problem with using func_num_args to emulate get/set?

    Bob wrote:
    > I'm using func_num_args to create a get/set type of function. Will
    > this have unpredictable results, or or lead to some sort of problem
    > I'm just not aware of? (I'm a bit of a newbie w/PHP.)
    >
    Not really, but it's a better practice to use defaults for the arguments:

    function var_name($value = "") {
    $this->name = $value;
    }

    If you want to set $name only when the argument isn't empty, you can do
    something like this:

    function var_name($value = "") {
    if ($value) {
    $this->name = $value;
    }
    }

    And now for naming; the function and variable names should represent there
    purpose. Since this is a setter which uses the argument to set a variable
    with the name $name, consider the following:

    function setName($name = "") {
    if ($name) {
    $this->name = $name;
    }
    }
    > $name = $my_obj->var_name(); // now set $name to 'Rob'
    >
    In proper class designs, setter and getter methods are seperated, so
    consider a method called getName() which returns the value of the $name
    variable instead:

    $name = $my_obj->getName(); // now set $name to 'Rob'


    HTH;
    JW



    Janwillem Borleffs Guest

  4. #3

    Default Re: Problem with using func_num_args to emulate get/set?

    Janwillem Borleffs wrote:
    > $name = $my_obj->getName(); // now set $name to 'Rob'
    >
    Of course, the comment should be:

    // now get $name


    JW



    Janwillem Borleffs Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139