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!
class someClass{
function someClass($id, $foo, $bar){
$this->id = $id;
$this->foo = $foo;
$this->bar = $bar;
//do whatever you need to initialize the class
}
function someMethod(){
//do something with properties
}
}
$foobar = new someClass(2, "Fooey", "barey");
$foobar->someMethod();
class someClass{
var $id;
var $foo;
var $bar;
function someClass(){
//do whatever you need to initialize the class
}
function someMethod(){
//do something with properties
}
}
$foobar = new someClass();
$foobar->id = 2;
$foobar->foo = "Fooey";
$foobar->bar = "Barey";
$foobar->someMethod();
Alright thanks. The first is the way I have always done it, but the second seems like it makes more sense. I am making a directory of my town's businesses and I would like to make a class to handle each business. I want the class to be able to add/remove/modify the business in the database as well as display the businesses in the directory in a number of diffrent ways and organizations. I will probably need some help, so this is why I am asking... more to come.
I was thinking more towards classes such as database abstractions..where the constructor is all you need to pass the arguments to, creating new variables is fine in the constructor (in my opinion).
Jcart wrote:I was thinking more towards classes such as database abstractions..where the constructor is all you need to pass the arguments to, creating new variables is fine in the constructor (in my opinion).
class dbMYSQL {
function dbMYSQL($user, $pass) {
$this->connection = mysql_connect('localhost',$user,$pass);
}
}
I see what your getting at, but I tend to use the first method to encapsulate the function as much as possible within it self.
Hmm... I don't like it personally.... Reason ==>
Objects have properties, those properties should always be there, they don't appear and disappear although they may change. Because that's in the constructor I can see your argument yes, and I won't say it's wrong. It would cause problems if you wanted to extend that class though, unless you force the parent constructor to run simply to create those properties.
Interesting read. In PHP at least some of those points fall down pretty quickly being loosley typed. It's always good to read other people's ideas on things like this. I guess that's how new design patterns ect come about in the first place
If the property is accessible to the outside without a get/set method, there's little to no security on it (unless you're using PHP 5), there's also no validation, or authorization code that can be run. It's about separation, to protect the object from outside code that shouldn't be touching internal properties..