1 / 25

Session 4: Object-Oriented Programming and Arrays

Session 4: Object-Oriented Programming and Arrays. iNET Academy Open Source Web Development. Objectives. Discuss object-oriented programming Create Classes Define methods Inheritance Discuss Arrays Create arrays Multidimensional arrays Extract variables from arrays

berget
Download Presentation

Session 4: Object-Oriented Programming and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 4: Object-Oriented Programming and Arrays iNET Academy Open Source Web Development

  2. Objectives • Discuss object-oriented programming • Create Classes • Define methods • Inheritance • Discuss Arrays • Create arrays • Multidimensional arrays • Extract variables from arrays • Array functions in PHP

  3. Object-oriented programming • Reuse code easier • Class • Attributes (Variables) • Methods (Functions) • Objects are instances created from a class

  4. Creating a Class and an Instance • cat: a class • $fluffy: an instance of cat

  5. Methods and Constructors • Methods are the functions defined within the class • Constructors are special methods which is called when a new instance of a class is created. Constructors is used to initialize class’s attributes. • Constructor may have parameters.

  6. Defining Methods & Instantiating new Instance • Constructor returns reference to the object. This reference can be assigned to a variable • The parentheses () is optional when the constructor has no parameter

  7. Variable Scope within Classes • Variable can have 3 scopes: • public: accessible from anywhere • private: accessible within the class • protected: acessible within subclass

  8. Inheritance • Subclass inherit attributes and methods from parent class (superclass) • Use extend keyword to declare a subclass

  9. The parent operator • To call parent class’s methods from subclass use parent::method_from_parent

  10. Static Methods and Variables • Methods and Variables can be defined as static • Use :: operator to refer to static variables and static methods

  11. Variable Reference • Use & operator to refer to the memory location of the variable • Using reference, we can have two variables refer to the same value

  12. Array Fundametals • Two new terms: elements and indexes • Elements: the values stored in the array. • Each element is refered by an index • Index value can be a number or a string, but must be unique • Associative vs. Numeric Indexed Arrays • Numeric arrays use numbers as indexes. First element start at zero • Associative arrays use strings as indexes. Can be used to store configuration data since their keys have meaningful name.

  13. Array Assignment • Via array identifiers • PHP does not report an error, but you can go through the array values sequentially

  14. Array Assignment (cont.) • Using array function

  15. Is_array function • Used to check whether a variable is an array

  16. Referencing array values • Accessing array values using the form $array_name[index] • Associative arrays referenced in a string must be enclosed in curly braces {}

  17. Looping through array values • Use foreach loop which automatically advances and reads each value from the array until it reaches the last value.

  18. Adding values to an array • To add values to the end of array, use the array identifier • To add values the an associative array, use similar syntax

  19. Counting elements in an array • Use count function or sizeof function

  20. Sorting arrays • Use the sort() function • Elements are arranged from lowest to highest • Numbers are sorted numerically • String are sorted alphabetically • All previous key are removed, new keys are assigned. • Use asort() function to sort associative array and maintain index association

  21. Multidimensional Arrays • Multidimensional arrays have other arrays as elements • Each set of keys and values represents a dimension

  22. Display Multidimensional Arrays • Use var_dump() function

  23. Extracting Variables from an Array • Use extract() function to place elements of an array into variables having the same names as the keys

  24. Building an Array from Variables

  25. Practice • In this practice, you will • Define a class named StaffInfo with the following attributes • ID • Name • DateOfBirth • Gender • Address • Telephone • Email • Use an array to store 10 objects typed StaffInfo. • Print the array in a table; same attributes are presented in a column.

More Related