Click to See Complete Forum and Search --> : objects


Da Warriah
08-23-2003, 09:16 AM
alright, this friend of mine that considers me a php guru (imagine that :o) talked to me on AIM last night and asked me what the "->" symbol did in PHP...i told him i didnt really know, ive really only seen that symbol once, and immediately went searching on http://www.php.net for the answer...i found it had something to do with objects, but either my brain was dead or it didnt explain it very well...so here i come once again, looking for an explanation...

what the heck are objects? and how do you use them? they look almost similar to arrays, only in a strange warped kind of way...any help would be appreciated:)

PunkSktBrdr01
08-23-2003, 03:58 PM
Based on what I read at php.net, the "->" symbol is used to reference things inside an object. Objects are created through classes. I've never used classes, so this is only what I've read. Anyway, you make a class className{ }, and then do new className to create the object. Classes are used to define variables, and can also hold functions. So if you do:


class varThing {
$thing = "thing";
}

$new_thing = new varThing;
$new_thing1 = $new_thing->thing;


Now $new_thing1 will equal "thing". I think that's how it works, anyway. Hope that helps.

Da Warriah
08-24-2003, 02:42 PM
alright, thanks for that, anyone else have any other knowledge to share? and why this would be used as opposed to an array?

pyro
08-24-2003, 10:16 PM
I really never program PHP in object oriented code, as PHP is not object oriented. So, I can't tell you 100% what that does, but I just wanted to let you know that you may get a better response from the CGI forums, as I'm assuming it would function the same in Perl.

Da Warriah
08-26-2003, 05:50 PM
alright, thanx, i just needed to know since im rewriting a script (adding and changing features along the way) and i came up with some objects...i think i should be able to stumble my way through it...;)

DaiWelsh
08-27-2003, 06:49 AM
Object-oriented programming is a book (or ten) in its own right, so I wont try to explain it all here, but in essence an object is a program entity that is treated like a black box, in that all the data inside it is kept safe from the outside world and should only be accessed using 'methods' or functions provided by the person who wrote the object.

For example, if you were writing a script that related to cars (automobiles to US citizens) you might create an object of type car which internally would have properties like speed, direction, fuel level etc. You are not permitted to directly change the fuel level or speed from your code outside the object, you can only call methods like maybe a Drive() method that moves the car forwards or a Turn() method that changes its direction or a FillUp() function that fills the fuel tank.

This may seem daft, but the concept of Object-Orientation is very powerful in that it allow one programmer to create a consistent set of rules and behaviours for an object and then other programmers can use it without needing to know how it works internally and without risking inadvertently breaking those rules.

The -> operator is used to refer to a method of an object so for example $car1->Turn(60) is a call to the Turn method in the object $car1 or $car2->FillUp() is a call to the FillUp() method of object $car2. This notation is necessary rather than just saying FillUp() beause you need to know which car to fill up (i.e. which object to call the method on).

As pyro says PHP as of v4 does not fully implement OO, as it allows you to do things you should not and does not provide all the standard OO functionality, however PHP v5 which is in beta takes it a lot further into the OO world.

If you want to understand more, just look for the million and one books and websites on OO (Object Orientation). From a practical stand point, you may not want to start OO programming until you know a bit more about it as it has a lot of consequences both good (organisation of large projects for one) and bad (performance usually) that you should be aware of.

HTH,

Dai

pyro
08-27-2003, 07:05 AM
Originally posted by DaiWelsh
...however PHP v5 which is in beta takes it a lot further into the OO world.Note that PHP5 is still not an object oriented language...

DaiWelsh
08-27-2003, 07:37 AM
Note that PHP5 is still not an object oriented language...

lol, true enough. It will a lot easier for people who wish to use object orientation in PHP, but it will not require anyone to know any OO in order to use PHP.

Long may that continue to be so. :)

Personally I use some OO principles for some of my work, but deliberately not all the principles and not for all projects. I have no desire to see PHP become 100% OO and removing that flexibility.

At least not unless h/w eventully outstrips the demands we make of it. :p

pyro
08-27-2003, 07:42 AM
I agree with that. As stated earlier, I really never program PHP using OO, as I've never had the need to. Also, unless something has changed that I'm not aware of, the PHP developers have no intention of making it an OO language... That is good, IMO... :)

Da Warriah
08-27-2003, 08:10 AM
alright, thanks for the explanation DaiWelsh, that was enough to turn me totally off of OO for the rest of my life ;) hehe just joking, but im thinking that if i havent learned it yet, after knowing PHP for about a year, then what i havent learned probably isnt important, hehe...ill try to decipher the objects documentation on php.net now that i have a bit more of an explanation, however i dont think ill be using anything like objects anytime soon...but thanks for the help:D