Objects / Aggregation pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
So we’ve added facilities to add and sell pets to our pet shop class, now let’s finally have a class that is going to show the stock of pets that are in stock at the moment, and so we’re simply just going to put in something to say, in bold it’s going to say shop stock and then a line break ‘we have’ and then we’re going to count the number of pets, so we’re going to count the pets array which will contain all the pet objects that we’ve created, then a couple of line breaks and then we’re going to iterate through the pets and we’re going to specify each of the pets that are stored in the pets array and we’re going to say pet number 'i' and then we’re going to add the report for the pet, which we can view by calling the particular element from the pets array and then we’re going to call the report method that we created in the class definition for animal. So each of the pets is going to include that, then we’re going to put at the bottom the price and we’re going to escape the dollar sign again using the back slash and then we’re going to say write down the pets price once again and then finally another line break. After that we’re going to pop in another line break for the sake of neatness and then that is going to be our pet shop class. That may look a little complicated but really all it is, is adding to the array, the pets array and creating an object, removing an object from the array and reporting on the stock of the pet shop. Now let’s call our pet shop 'shop' not a very imaginative name but there we go, new pet shop and we’re going to say shop, I give it a space there, we’re going to say ‘shop add pet’ and now we just type in, as we can see the add pet took three arguments and that was the name of the class, the pets price and the pets color. So we’re going to say, you are going to have a cat and that's going to be 20 dollars and it’s going to be a tabby cat, then we’re going to add pet. Now this ones is going to be a snake 40 and that's going to be a brown snake there. We’re also going to add, maybe let’s add, let’s have 2 snakes in fact. Let’s have a more expensive snake, a more expensive black snake and then we’re going to try out our show pets function, Let’s sell one our pets after we’ve shown the pets. Let’s sell the first pet, which is the tabby cat, then we’re going to show the stock again. Now that's a lot to typing without testing, not something that I recommend you do, of course I’ve tested this script before, which is why I’m doing it but its much better practice to test our each little tiny bit of the code as you are typing it, then if there’s any errors, you will see exactly why. Lets’ pop over to our browser though and take a look at the way our pet shop is working, as we can see, our first function, the show pets function shows that we have 3 pets for sale in the shop, the cat was sold and then there are only the 2 snakes left as the stock in the pet shop. This is an example of aggregation, the pet shop contains a number of objects within itself that are not related to it by inheritance and it manipulates those objects by calling it’s own methods and using them on those objects and it also calls the methods of those objects themselves. Once again this is quite a lot to digest in this chapter, so I really recommend that if you’re going to use any of these methods that you try all you examples out for yourself, take a look at the source code included with this chapter and try to make your own examples of an object oriented structure of your choice. In our next chapter, we’re going to create a sample php app.
Tutorial Information
| Course: | PHP |
| Author: | Joshua Mostafa |
| SKU: | 33332 |
| ISBN: | 1889347787 |
| Release Date: | 2002-03-26 |
| Duration: | 9.5 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 