Web Systems Development (CSC-215) Lecture 4: Functions and Objects Monday 12th February 2018
Class Test 1 Monday 19th February Lab based – bring laptops or have desktop ready Lectures 1-6 Duration: 30 minutes
Setting up netbeans with wampserver for debugging
A note on type casting
Implicit & Explicit Casting PHP is loosely typed Variable and its type is declared simply by using it Automatically converts types when needed, via implicit casting
What if this behavior causes problems in some situations? We need a way to override. What if this behavior causes problems in some situations?
Explicit Casting Script produces a float value (attempts to produce the most accurate result) What if you need an integer value?
Explicit Casting
PHP Cast Types
Php functions & objects
Defining a Function
Example: Cleaning up a full name
Returning an array
Passing by reference
Including & requiring files
Include Statement
Include Once
Require and require once Program continues to execute even if the include failed (will then fail where it was needed)
Check if function exists Because PHP is evolving rapidly
Php objects
Creating Objects
Accessing Objects
Cloning Objects Assigning will create an additional reference
Not working
Use clone to create new instances
Constructors
Destructors
This
Static methods Called on class, not object
Implicit property definition - allowed by PHP but bad programming practice
Constants in class
Property & Method Scope Public Outside code accesses Extending classes should inherit Protected Outside code should not access Private Extending classes should not inherit
Usage
Inheritance Deriving subclasses from pre-existing classes Only modify the parts that are different Using extends operator
The parent operator A method in a subclass with the same name as one in its parent class will override the parent class method The parent operator can be used to access the parent method if this behavior is not desired
Parent constructors must be called explicitly when needed
Prevent overriding by subclass
Lecture content adapted from chapter 5 of Learning PHP, MySQL, JavaScript, CSS & HTML5, 3rd Edition, by Robin Nixon.