PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
As a rule you should always use setters and getters. The whole point of a class is to encapsulate functionality behind an interface. Using setters and getters rather than acessing vars directly makes the interface much more clearly defined.
However, it can be useful to have "method-less" classes ie just a bunch of properties and no class functions. In this case, getters and setters don't add anything to the usability of the class - in fact they'd just create clutter.
At least that's my opinion anyway.
Incidentally, a singleton (with its "superglobal" scope) can be handy for a script monitoring /errors object. Again, singletons are best where you've got quite complex object relationships and it's a pain to try to pass the object around as an arg.
I see. I noticed in the script you rewrote, you used setEmail(), serUsername() or some function of that sort to set the variable. What difference does it make of using setVar('username', 'nay'); ?
Anyhow, I noticed after functions you describe it's operations as void, boolean. Might sound wierd but what do void and boolean actually mean? What it returns? Eg: int, str.
Nay wrote:I see. I noticed in the script you rewrote, you used setEmail(), serUsername() or some function of that sort to set the variable. What difference does it make of using setVar('username', 'nay'); ?
<?php
Class Foo {
var $bar = 'bar';
var $foo2 = 'foo2';
}
// first mehtod
function setVar($name, $value) {
if(isSet($this->$name)) {
$this->$name = $value;
}
}
// another method
function setFoo2($value) {
$this->foo2 = $value;
}
?>
I think I saw on Zend while I was reading an article on OOP. I'm trying to hunt it down heh. Anyhow, why use the second method way? To create a method just to set one var? (I saw it on Zend so there must be some 'good' reason).
Setters tell an object to change state and separate setters (or getters) create a more clearly defined interface. It is the best way.
I suppose if you had a lot of properties to set simply by declaring $this->property = $fn_arg it might be good to have a single method. "Resolve repeated code into a single source" is a fundamental refactoring.
It's also possible that a class doesn't know what properties have been set in itself. You'd have to use a generic getter similar to the setVar method above.
For example, I always access $_POST vars via a Firewall class which filters out anything which fails to validate. Since different forms submit different fields I can't hard code getters in Firewall.
more propper? Because I see the second way used more in articles etc. But why define a local variable, that does not have much of any re-use. Since I'd only need to connect to the MySQL server once.
Also, while I was starting on my 'framework' today - In an ametuer UML format so far. Anyhow, I was wondering how to 'check' arguments in methods. Otherwise, an easier way (hopefully shorter way) of doing:
<?php
function Foobar($string, $int) {
$msg = 'Warning: Argument passed for Foobar is not valid';
if(!is_string($string)) {
die($msg);
}
if(!is_int($int)) {
die($msg);
}
}
// So:
Foobar(1, "2"); // Would throw an error
Foobar("boohoo", 10); // Would not
?>