3-Classes and Functions
3-Classes and Functions
1. Classes in PHP
Classes are blueprints for creating objects in PHP and play a
central role in object-oriented programming (OOP). They help
organize code by encapsulating related data and methods.
Components of a Class:
1. Properties: Variables defined within a class that hold
data relevant to the object.
2. Methods: Functions inside the class that define its
behavior.
3. Constructor: A special method invoked when an object
is created, useful for initializing properties.
4. Access Modifiers: Keywords like public, private, and
protected, controlling access to class properties and
methods.
2. Regular Expressions (Regex)
Regular expressions (Regex) in PHP allow you to search for
and manipulate strings based on specific patterns, making
them ideal for validation, parsing, and complex text
manipulation.
Common Uses:
• Form Validation: Validating email formats, phone
numbers, etc.
• Pattern Matching: Searching within a string for specific
patterns.
• String Manipulation: Replacing parts of a string based
on a matching pattern.
Key Regex Functions in PHP:
• preg_match() – Checks if a string matches a pattern.
• preg_replace() – Replaces parts of a string based on a
pattern.