Page 1 of 2

Classes

Posted: Wed Nov 09, 2005 4:05 pm
by Luke
I am confused at the syntax of classes and how they should be executed.
Which is the way classes are supposed to be done?

This one:

Code: Select all

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();
Or this one:

Code: Select all

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();

Posted: Wed Nov 09, 2005 4:07 pm
by Chris Corbyn
Technically, the second snippet you posted is correct.

The first one will work but it's not supposed to be handled in that way ;)

EDIT | As a side note since you appear to be just starting out :)

Code: Select all

$foobar->id = 2;
$foobar->foo = "Fooey";
This *generally* is not a good idea... ideally we want to use "getters" and "setters".

Code: Select all

class someClass
{
    var $id;
    var $foo;

    function someClass()
    {
        //Construct
    }

    function getId()
    {
        return $this->id;
    }

    function setId($id)
    {
        $this->id = $id;
    }

    function getFoo()
    {
        return $this->foo;
    }

    function setFoo($foo)
    {
        $this->foo = $foo;
    }
}

$class = new someClass();
$class->setFoo('Value here');
$class->setId(32);

echo $class->getFoo();
echo $class->getId();
That's more obvious in PHP5 where you'll use "private", "protected" and "public" properties ;)

Posted: Wed Nov 09, 2005 4:09 pm
by John Cartwright
d11wtq wrote:Technically, the second snippet you posted is correct.

The first one will work but it's not supposed to be handled in that way ;)
What is wrong about the first one??

Posted: Wed Nov 09, 2005 4:13 pm
by Chris Corbyn
Jcart wrote:
d11wtq wrote:Technically, the second snippet you posted is correct.

The first one will work but it's not supposed to be handled in that way ;)
What is wrong about the first one??
Creating properties on-the-fly, without defining them first.

EDIT | I never actually said it was "wrong"... I just said it's not supposed to be done like that but it will work :D

Posted: Wed Nov 09, 2005 4:14 pm
by Luke
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.

Posted: Wed Nov 09, 2005 4:23 pm
by John Cartwright
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).

Code: Select all

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.

Posted: Wed Nov 09, 2005 4:33 pm
by Chris Corbyn
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).

Code: Select all

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.

Posted: Wed Nov 09, 2005 4:39 pm
by Luke
I'm gonna set up my class and post it here for you guys to proof... k?

Posted: Wed Nov 09, 2005 5:09 pm
by Roja
d11wtq wrote: This *generally* is not a good idea... ideally we want to use "getters" and "setters".
For an interesting counter-point, there is a well-written article on why "getters and setters" are non-ideal.

Just thought I'd mention it.

Posted: Wed Nov 09, 2005 5:25 pm
by Chris Corbyn
Roja wrote:
d11wtq wrote: This *generally* is not a good idea... ideally we want to use "getters" and "setters".
For an interesting counter-point, there is a well-written article on why "getters and setters" are non-ideal.

Just thought I'd mention it.
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 :D

Posted: Wed Nov 09, 2005 6:06 pm
by Luke
I don't understand...
Why would you need to use getters and setters??

Posted: Wed Nov 09, 2005 6:14 pm
by feyd
for "proper" object oriented interaction, direct access to properties is, more often than not, bad..

Posted: Wed Nov 09, 2005 6:14 pm
by Luke
well I understand that it is bad... my question was.. why?

Does anybody have a class they wrote that they don't mind letting me take a look at?

Posted: Wed Nov 09, 2005 6:20 pm
by feyd
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..

Posted: Wed Nov 09, 2005 6:21 pm
by Luke
OK so there should be some type checking in a setter?
Do you recommend using getters/setters?