[ Pobierz całość w formacie PDF ]
.You learned how to manipulate arrays byadding or removing multiple elements.Finally, you learned some of the techniquesthat PHP4 makes available to sort arrays.Q&AQ If the foreach statement was introduced with PHP4, how didprogrammers using PHP3 iterate through arrays?A The PHP3 technique for looping through an array involved a function called each(),which was used in conjunction with a while statement.You can read about thistechnique inHour 16.Q Are there any functions for manipulating arrays that we have not125covered here?A PHP4 supports many array functions.You can read about some more of these inHour 16 and find them all in the official PHP manual athttp://www.php.net/manual/ref.array.php.Q I can discover the number of elements in an array, so should I use a forstatement to loop through an array?A You should be cautious of this technique.You cannot be absolutely sure that thearray you are reading is indexed by consecutively numbered keys.WorkshopThe Workshop provides quiz questions to help you solidify your understanding of thematerial covered.Try to understand the quiz answers before continuing to the nexthour's lesson.Quiz answers are provided in Appendix A.QuizWhat function can you use to define an array?What is the index number of the last element of the array defined below?$users = array("Harry", "Bob", "Sandy");Without using a function, what would be the easiest way of adding the element"Susan" to the $users array defined previously?Which function could you use to add the string "Susan" to the $users array?How would you find out the number of elements in an array?In PHP4, what is the simplest way of looping through an array?What function would you use to join two arrays?How would you sort an associative array by its keys?ActivitiesCreate a multidimensional array of movies organized by genre.This should takethe form of an associative array with genres as keys ("SF", "Action", "Romance",and so on).Each of this associative array's elements should be an arraycontaining movie names ("2001", "Alien", "Terminator", and so on).Loop through the array you created in Activity 1, outputting each genre and itsassociated movies to the browser.126Hour 8: ObjectsOverviewObject-oriented programming is dangerous.It changes the way you think aboutcoding, and once the concepts have a hold on you, they don't let go.PHP, like Perlbefore it, has progressively incorporated more object-oriented aspects into itssyntax and structure.With the advent of PHP4, it becomes possible to useobject-oriented code at the heart of your projects.Throughout this hour, you'll take a tour of PHP4's object-oriented features and applythem to some real-world code.In this hour, you will learnWhat objects and classes areHow to create classes and instantiate objectsHow to create and access properties and methodsHow to create classes that inherit functionality from othersSome of the reasons why object-oriented programming can help you to organizeyour projectsWhat Is an Object?An object is an enclosed bundle of variables and functions forged from a specialtemplate called a class.Objects hide a lot of their inner workings away from thecode that uses them, providing instead easy interfaces through which you can sendthem orders and they can return information.These interfaces are special functionscalled methods.All the methods of an object have access to special variables calledproperties.By defining a class, you lay down a set of characteristics.By creating objects of thattype, you create entities that share these characteristics but might initialize them asdifferent values.You might create an automobile class, for example.This classwould have a color characteristic.All automobile objects would share thecharacteristic of color, but some would initialize it to "blue," others to "green," andso on.A class is a collection of special functions called methods and special variables calledproperties.You can declare a class with the class keyword.Classes are thetemplates from which objects are created.NEW Existing in memory rather than as code, an object is an instance of a127TERM class.That is, an object is the working embodiment of thefunctionality laid down in a class.An object is instantiated with thenew statement in conjunction with the name of the class of which itis to be a member.When an object is instantiated, you can access allits properties and all its methods.Perhaps the greatest benefit of object-oriented code is its reusability.Because theclasses used to create objects are self-enclosed, they can be easily pulled from oneproject and used in another
[ Pobierz całość w formacie PDF ]