Ask a Question related to PHP Development, Design and Development.
-
Bob #1
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
-
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... -
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... -
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... -
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... -
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... -
Janwillem Borleffs #2
Re: Problem with using func_num_args to emulate get/set?
Bob wrote:
Not really, but it's a better practice to use defaults for the arguments:> 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.)
>
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;
}
}
In proper class designs, setter and getter methods are seperated, so> $name = $my_obj->var_name(); // now set $name to 'Rob'
>
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
-
Janwillem Borleffs #3
Re: Problem with using func_num_args to emulate get/set?
Janwillem Borleffs wrote:
Of course, the comment should be:> $name = $my_obj->getName(); // now set $name to 'Rob'
>
// now get $name
JW
Janwillem Borleffs Guest



Reply With Quote

