0% found this document useful (0 votes)
25 views

3-Classes and Functions

Php notes

Uploaded by

Leslie Qwer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

3-Classes and Functions

Php notes

Uploaded by

Leslie Qwer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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.

3. Working with Dates and Times


PHP provides functions to handle dates and times, making it
easier to format, manipulate, and calculate dates.
Key Date/Time Functions:
1. date(): Formats a timestamp as a human-readable date.
2. time(): Returns the current Unix timestamp.
3. mktime(): Creates a timestamp for a specific date and
time.
4. DateTime Class: An advanced, object-oriented way to
manage dates and times, offering methods for
manipulating and formatting.
4. Code Reuse with require() and include()
To promote code reuse and maintainability, PHP provides
require() and include() functions, allowing you to import code
from other files.
Differences:
• require(): Includes the file and stops execution if the file
is not found.
• include(): Includes the file but does not stop execution if
the file is missing.
Both functions help modularize your code, especially when
reusing headers, footers, or configuration files across multiple
pages.
5. Filesystem Functions
PHP’s filesystem functions allow you to interact with the file
system, enabling tasks like reading from and writing to files,
managing directories, and retrieving file information.
Common Filesystem Functions:
• file_exists(): Checks if a file or directory exists.
• fopen() / fclose(): Opens and closes a file.
• fread() / fwrite(): Reads from and writes to a file.
• unlink(): Deletes a file.
6. File Input and Output (I/O)
File I/O in PHP involves reading from and writing data to files,
which is critical for handling data persistence, logging, and
file-based data exchange.
Reading and Writing Files:
• Reading: Use functions like fread(), file_get_contents(),
or fgets() for reading file content.
• Writing: Functions like fwrite() and file_put_contents()
allow you to write data to files.
7. File Uploads
PHP allows you to upload files from the client to the server
securely. Handling file uploads involves validating file types,
sizes, and storing them in the designated directory.
Process:
1. Form Submission: A form with
enctype="multipart/form-data" is required for file
uploads.
2. Server Processing: Use the $_FILES array to access the
uploaded file information.
3. Validation and Storage: Validate file size, type, and
move the file to the server directory using
move_uploaded_file().
8. Error Handling and Logging
PHP’s error handling allows you to manage unexpected
conditions gracefully and log errors for debugging.
Types of Errors:
1. Syntax Errors: Mistakes in the code structure, like
missing semicolons.
2. Runtime Errors: Occur during program execution, like
division by zero.
3. Logical Errors: Errors in the logic leading to incorrect
results.
Error Handling Techniques:
• try...catch: PHP’s try-catch blocks handle exceptions
gracefully.
• error_log(): Logs error messages to a file or sends them
via email for debugging.
• set_error_handler(): Custom error handler to define
how specific errors are managed.
9. Sending Emails
PHP’s mail() function enables you to send emails directly
from your server, useful for notifications, form submissions,
and password resets.
Key Parameters for mail():
• Recipient: The recipient’s email address.
• Subject: The email subject line.
• Message: The main content of the email.
• Headers: Additional headers, like From, Cc, and Bcc.

You might also like