Page 2 of 2
Posted: Tue Mar 23, 2004 3:12 pm
by Nay
Ah I see. So then I would be doing:
Code: Select all
<?php
$config =& singleton('Config');
// usage
$link = mysql_connect('localhost', $config->mysql_user, $config->mysqlpass);
?>
Am I doing it right or should I be using setters and getters?
-Nay
Posted: Tue Mar 23, 2004 3:41 pm
by McGruff
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.
Posted: Tue Mar 23, 2004 3:48 pm
by Nay
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'); ?
Edit: I was also looking at:
http://www.holub.com/goodies/uml/index.html
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
Posted: Tue Mar 23, 2004 4:04 pm
by magicrobotmonkey
yea void means no return and boolean means it returns a boolean (true/false)
Posted: Tue Mar 23, 2004 4:56 pm
by McGruff
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'); ?
Not sure what you're asking.
Posted: Wed Mar 24, 2004 1:58 am
by Nay
Okay, say:
Code: Select all
<?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).
-Nay
Posted: Wed Mar 24, 2004 4:27 am
by McGruff
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.
Posted: Wed Mar 24, 2004 5:54 am
by Nay
Okay thanks for all the help. I'm going to rewrite the class(es) again - with my 'newly' learned things. Hopefully it's a bit more propper OOP.
Another question, is this way:
Code: Select all
<?php
$config =& singleton('Config');
// usage
$link = mysql_connect('localhost', $config->getVar('mysql_user'), $config->getVar('mysqlpass'));
?>
or
Code: Select all
<?php
$config =& singleton('Config');
// usage
$mysql_user = $config->getVar('mysql_user');
$mysql_pass = $config->getVar('mysql_pass');
$link = mysql_connect('localhost', $mysql_user, $mysql_pass);
?>
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.
-Nay
Posted: Wed Mar 24, 2004 6:15 pm
by McGruff
Dosn't really make much difference. Possibly the second is slightly easier to assimilate but I'd tend to go for the first.
Posted: Thu Mar 25, 2004 6:56 am
by Nay
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:
Code: Select all
<?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
?>
-Nay