Functions & Objects: Free PHP & MySQL Tutorial
Master PHP Functions and Object-Oriented Programming Fundamentals
Core Concepts You'll Master
Functions & Arguments
Learn to create reusable code blocks that accept dynamic inputs for flexible programming solutions.
Objects & Properties
Understand how to structure data using object-oriented principles with properties and methods.
Class Inheritance
Discover how to extend existing classes to build upon functionality without rewriting code.
Functions can include alphabetical or numerical characters, cannot begin with a number, and cannot include spaces but can use underscores for readability.
Creating Your First Function
Define the Function
Start with the keyword 'function' followed by the function name and parentheses
Add Function Body
Place your code logic between curly braces that will execute when called
Return a Value
Use the 'return' keyword to specify what value should be passed out of the function
Call the Function
Use the function name with echo to display results or store in variables
Function Calls: With vs Without Arguments
| Feature | Without Arguments | With Arguments |
|---|---|---|
| Flexibility | Fixed values only | Dynamic inputs |
| Reusability | Limited | High |
| Code Efficiency | Requires multiple functions | One function, multiple uses |
| Example Output | Always 300 | 300, 240, 200, 400 |
Lawyer Bill Calculations from Tutorial
PHP class names use CamelCase formatting, stringing words together while capitalizing each word. The first letter may be capital or lowercase.
Objects vs Multidimensional Arrays
The keyword 'new' is always used to initialize a new PHP object of any kind
Building a Custom Class
Declare the Class
Use 'class' keyword followed by CamelCase class name in curly braces
Define Properties
Add public or private variables that will store object data
Create Methods
Write functions inside the class that can access object properties using '$this'
Instantiate Objects
Create new instances using 'new' keyword and set property values
Public vs Private Properties
| Feature | Public Properties | Private Properties |
|---|---|---|
| Access Level | Easily accessed externally | Only accessible within class |
| Setting Values | Direct assignment | Through methods only |
| Security | Less controlled | More secure and controlled |
| Common Usage | Simple objects | Professional PHP applications |
The __construct method requires two underscores before 'construct' and is automatically called when creating new object instances with the 'new' keyword.
Extended classes automatically inherit all properties and methods from their parent class without needing to redefine existing functionality.
Class Extension Process
Create Base Class
Define original Lawyer class with basic properties and methods
Extend the Class
Use 'extends' keyword to create BillboardLawyer that builds on Lawyer
Override Methods
Redefine specific methods while keeping others unchanged
Call Parent Methods
Use 'parent::method_name()' to access original functionality
Key Takeaways