250 likes | 362 Views
In this session, we explore fundamental concepts of Object-Oriented Programming (OOP) and Arrays in PHP. Learn how to create classes, define methods, and utilize inheritance. We will also cover essential array manipulation techniques, including creating arrays, extracting variables, and utilizing multidimensional arrays. Gain practical experience by defining a class for staff information and storing multiple objects in an array, culminating in displaying the data in a structured format.
E N D
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 • Array functions in PHP
Object-oriented programming • Reuse code easier • Class • Attributes (Variables) • Methods (Functions) • Objects are instances created from a class
Creating a Class and an Instance • cat: a class • $fluffy: an instance of cat
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.
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
Variable Scope within Classes • Variable can have 3 scopes: • public: accessible from anywhere • private: accessible within the class • protected: acessible within subclass
Inheritance • Subclass inherit attributes and methods from parent class (superclass) • Use extend keyword to declare a subclass
The parent operator • To call parent class’s methods from subclass use parent::method_from_parent
Static Methods and Variables • Methods and Variables can be defined as static • Use :: operator to refer to static variables and static methods
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
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.
Array Assignment • Via array identifiers • PHP does not report an error, but you can go through the array values sequentially
Array Assignment (cont.) • Using array function
Is_array function • Used to check whether a variable is an array
Referencing array values • Accessing array values using the form $array_name[index] • Associative arrays referenced in a string must be enclosed in curly braces {}
Looping through array values • Use foreach loop which automatically advances and reads each value from the array until it reaches the last value.
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
Counting elements in an array • Use count function or sizeof function
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
Multidimensional Arrays • Multidimensional arrays have other arrays as elements • Each set of keys and values represents a dimension
Display Multidimensional Arrays • Use var_dump() function
Extracting Variables from an Array • Use extract() function to place elements of an array into variables having the same names as the keys
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.