manix Posted July 21, 2011 Share Posted July 21, 2011 Hey, Since I haven't been practicing any other programming languages before (except pascal but there I used no classes and VB where the classes were automatically declared) now I've come across classes in PHP and I am not really aware of their purpose and what they're supposed to do.. I tried googling it but I couldn't find any good article about it, if someone could provide me one I'd be really greatful, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/ Share on other sites More sharing options...
trq Posted July 21, 2011 Share Posted July 21, 2011 Classes are used to group together and encapsulate functionality and any data this functionality might need to do it's job. You should read this: http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect It is the first of a 3 part series which is well worth reading. Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245563 Share on other sites More sharing options...
manix Posted July 21, 2011 Author Share Posted July 21, 2011 Erm.. Okay but what's the point of grouping objects when you can simply not ? It's getting so complicated I don't get the point of classes O.o Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245639 Share on other sites More sharing options...
ignace Posted July 21, 2011 Share Posted July 21, 2011 Like thorpe said classes encapsulate behavior and state. Without classes you would be looking at global variables and functions that manipulate those variables. However you do not control access to those global variables so the variables your functions depend on can be modified by any 3rd party script you introduce since both you and the 3rd party script happen to have a $user variable. To avoid any name collisions you would use a class or now with 5.3 namespaces (although namespaces are not entirely safe). Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245643 Share on other sites More sharing options...
Maknib Posted July 21, 2011 Share Posted July 21, 2011 Erm.. Okay but what's the point of grouping objects when you can simply not ? It's getting so complicated I don't get the point of classes O.o Classes are blueprints. say an architect makes a blueprint for a house and wants to create 50 of them houses.. he is not going to 50 separate blueprints for the same house. say you're making a game, you make a player that can jump swim walk run and attack. 2 months later you make another game with a player that does the exact same thing. are you going to want to rewrite the code or simple use a class that already exists. so your player class has methods call jump swim walk attack...etc. all you would need to do is something like (this is not php code..) New player = New PlayerClass(); now to get him to jump you would do.. player.jump(); or.. if(player touches water){ player.swim(); } and so on not sure with PHP but something like Actionscript or Java if you want to round 23.34 down you would type Math.floor(23.34); Math is a abstract class and floor is the method of the class... if there were no classes you would have to create the code to do this every time you needed it. if you are starting out do not dive straight into classes and oop until you have a strong feel and understanding of the code... im still learning myself so most of this is probably wrong Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245650 Share on other sites More sharing options...
ignace Posted July 21, 2011 Share Posted July 21, 2011 In procedural you would write something like: function login($username, $password) { global $user; if (login successfull) { $user = $userdata; } } In OO you would write: class User { private $data; public function login($username, $password) { if (login successfull) { $this->data = $userdata; } } } The difference is that in procedural: 1. You do not control access to the $user variable which allows name collissions with 3rd party scripts and thus unexpected/uncontrolled behavior in your code. 2. Your function becomes highly dependent on the context it is used in (only works if a $user variable exists). This is also why they advice you not to use global variables inside your functions. Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245653 Share on other sites More sharing options...
manix Posted July 21, 2011 Author Share Posted July 21, 2011 Those were exactly the answers I needed, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/242520-classes/#findComment-1245669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.