M.O.S. Studios Posted June 20, 2009 Share Posted June 20, 2009 Ive been looking up classes on the php manual, I am a little confused what the use is for. The only definition the manual gives is that it was completely redone for php 5(which is what i use), can any one explain it to me??? Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/ Share on other sites More sharing options...
RussellReal Posted June 20, 2009 Share Posted June 20, 2009 a class is like an object, that is replicatable for example: class Cat { var $name; function getName() { return $this->name; } } $cat1 = new Cat(); $cat1->name = 'Tabby'; $cat2 = new Cat(); $cat2->name = "Bob"; $cat1->getName()." loves talking to ".$cat2->getName()."!"; all it does is allow you to give multiple things the same proceedures, but you can make every single instance of those proceedures,functions,variables, unique to that specific instance Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860316 Share on other sites More sharing options...
.josh Posted June 20, 2009 Share Posted June 20, 2009 a class is like an object, that is replicatable no not really... a class is like a blueprint/design/description for an object. Like for instance, if you want to define a dog. Things, or 'properties' of a dog: - name - size - weight - color - breed Things, or 'methods' a typical dog might do/have: - eat - poop - sleep So these things about a dog would be described in a class, and for example, your little dog muffy would be an instance of that dog class. Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860360 Share on other sites More sharing options...
RussellReal Posted June 20, 2009 Share Posted June 20, 2009 a class IS basically you're right a blueprint.. but when you instantiate lets say Dog() its essentially a Dog... but you're right Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860366 Share on other sites More sharing options...
M.O.S. Studios Posted June 20, 2009 Author Share Posted June 20, 2009 Thanks for the replies I just don't get it, I'm thinking its because I don't understand the application aspect of it Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860376 Share on other sites More sharing options...
RussellReal Posted June 20, 2009 Share Posted June 20, 2009 ok.. look @ it this way.. pretend your php application had a party lol.. and they were counting guests.. <?php class Guest { public $firstName, $lastName, $carColor, $carMake, $age; public function __construct($fn,$ln,$cc,$cm,$a) { $this->firstName = $fn; $this->lastName = $ln; $this->carColor = $cc; $this->carMake = $cm; $this->age = $a; } } $guests = array(); $guests[] = new Guest('Billy','Joel','blue','Audi',19); $guests[] = new Guest('Billy','Joel','black','Mercedes',29); $guests[] = new Guest('Darnel','Jones','red','Ford',14); $guests[] = new Guest('Wayne','Taylor','silver','Lincoln',24); foreach ($guests as $k => $guest) { echo 'Guest '.$k.' is '.$guest->age.' with a '.$guest->carColor.' '.$guest->carMake.'!?!?!?! HOW??<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860390 Share on other sites More sharing options...
ollie007 Posted June 20, 2009 Share Posted June 20, 2009 is a fancy name for a collection of functions, with an output thats changeable depending on what you feed it Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860396 Share on other sites More sharing options...
RussellReal Posted June 20, 2009 Share Posted June 20, 2009 errr.. if it helps you understand it better by understanding it that way, go for it.. but I wouldn't say thats exactly the right way to look at it.. just learn how to use em lol Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860410 Share on other sites More sharing options...
M.O.S. Studios Posted June 21, 2009 Author Share Posted June 21, 2009 i get it, its basically an array (a multi array in the example you gave). the main difference is that you can use php to update its value every time its called. i think i got it now, AWESOME!!!! Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860434 Share on other sites More sharing options...
RussellReal Posted June 21, 2009 Share Posted June 21, 2009 not exactly again... its not an array at all.. I just showed you a practical use for it Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860494 Share on other sites More sharing options...
Philip Posted June 21, 2009 Share Posted June 21, 2009 is a fancy name for a collection of functions, with an output thats changeable depending on what you feed it Kinda. Typically a class will be an organized structure of methods (functions), properties (variables), and other objects to perform a specific task or a range of tasks. I added a bit to the Guest class: <?php // Our Guest Class: class Guest { // Setup the properties of the class: public $firstName, $lastName, $carColor, $carMake, $age, $cups, $bags; // Add what happens when you create a new guest public function __construct($fn,$ln,$cc,$cm,$a) { // Set their properties $this->firstName = $fn; // Get this from the passed arguments $this->lastName = $ln; // Get this from the passed arguments $this->carColor = $cc; // Get this from the passed arguments $this->carMake = $cm; // Get this from the passed arguments $this->age = $a; // Get this from the passed arguments $this->cups = 0; // Set this to 0 as default $this->bags = 0; // Set this to 0 as default } // What if our guests get thirsty? public function drankSoda($cups) { // We should keep track of how many sodas they have $this->cups = $this->cups + $cups; } // But then they might get hungry too! public function ateChips($bags) { // So let's keep tally of who eats the most at my party $this->bags = $this->bags + $bags; } } // We normally would create an object like this: $Phil = new Guest('Phil', 'IsCool', 'green', 'Mini', 19); // Then, we could say I drank 2 cups of soda: $Phil->drankSoda(2); // I then got super hungry and had 5 bags of chips: $Phil->ateChips(5); // Finally, I had to wash those chips down and had one more cup of soda: $Phil->drankSoda(1); // In the end, let's see how I did: echo $Phil->firstName.' '.$Phil->lastName.' drove up in a '.$Phil->carColor.' '.$Phil->carMake.' and had '.$Phil->cups.' cups of soda and '.$Phil->bags.' bags of chips!'; // That would show on screen: // Phil IsCool drove up in a green Mini and had 3 cups of soda and 5 bags of chips. ?> Maybe that will make more sense? Quote Link to comment https://forums.phpfreaks.com/topic/163041-what-is-a-class-for/#findComment-860502 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.