Classes

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!

Moderator: General Moderators

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Classes

Post 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();
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Last edited by Chris Corbyn on Wed Nov 09, 2005 4:12 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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??
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Last edited by Chris Corbyn on Wed Nov 09, 2005 4:14 pm, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm gonna set up my class and post it here for you guys to proof... k?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I don't understand...
Why would you need to use getters and setters??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for "proper" object oriented interaction, direct access to properties is, more often than not, bad..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

OK so there should be some type checking in a setter?
Do you recommend using getters/setters?
Post Reply