PHP | Basics

Basics of PHP


# Access Modifiers

different levels of visibility or access modifiers. These access modifiers define how and where the function can be accessed:
  1. Public:

    • Functions declared as public can be accessed from anywhere: inside the class, from other classes, and from outside the class.

    class MyClass { public function myPublicFunction() { echo "This is a public function."; } } $obj = new MyClass(); $obj->myPublicFunction(); // This will work.
  2. Protected:

    • Functions declared as protected can be accessed only within the class itself and by classes derived from that class (i.e., subclasses). They cannot be accessed from outside the class.
    class MyClass {
    protected function myProtectedFunction() { echo "This is a protected function."; } } class MySubClass extends MyClass { public function accessProtected() { $this->myProtectedFunction(); // This works, since it's within a subclass. } } $obj = new MySubClass(); $obj->accessProtected(); // Works. $obj->myProtectedFunction(); // Error: Cannot access protected function.
  3. Private:

    • Functions declared as private can only be accessed within the class in which they are defined. They cannot be accessed from derived classes or from outside the class.

    class MyClass { private function myPrivateFunction() { echo "This is a private function."; } public function accessPrivate() { $this->myPrivateFunction(); // This works, since it's inside the same class. } } $obj = new MyClass(); $obj->accessPrivate(); // Works. $obj->myPrivateFunction(); // Error: Cannot access private function.

Summary:

  • Public: Accessible everywhere.
  • Protected: Accessible only within the class and its child classes.
  • Private: Accessible only within the class where it's defined.

These access modifiers allow for encapsulation and control over how class members (both properties and methods) can be accessed and modified.



Debugging Methods

Debugging: Debug in PHP refers to the process of identifying, analyzing, and resolving errors, bugs, and unexpected behaviors in PHP code. It is an essential skill for PHP developers to ensure their applications function correctly and deliver the desired results.

During the development cycle, PHP code may encounter various issues such as syntax errors, logical errors, runtime errors, or unexpected behavior. Debugging helps developers locate and rectify these problems to improve the overall functionality and performance of their applications.

Runtime Error: A runtime error in a program is an error that occurs while the program is running after being successfully compiled. Runtime errors are commonly called referred to as “bugs” and are often found during the debugging process before the software is released
A runtime error is an issue that occurs when a program is being executed, causing it to malfunction. Runtime errors can be caused by a number of things, including: Coding mistakes, Memory corruption, Incorrect system configuration, Invalid instructions, and Peripheral devices not turned on.

Basic technique in PHP Debugging:

4. Debugging with PHP Error Log
3. Debugging with var_dump() and print_r()


# Types of variable in PHP

1. User-Defined Functions

  • These are functions that you, as the developer, create to perform specific tasks.

function sayHello() { echo "Hello, World!"; } sayHello(); // Outputs: Hello, World!

2. Built-in Functions

  • PHP comes with a wide range of built-in functions to handle various operations like string manipulation, file handling, date/time, etc.

$length = strlen("Hello"); // strlen() is a built-in function to get string length echo $length; // Outputs: 5

3. Anonymous (or Lambda) Functions

  • Functions without a specified name, often used as callbacks or for short, temporary tasks. These can be assigned to variables and passed around like other data types.
$greet = function($name) {
return "Hello, $name!"; }; echo $greet("John"); // Outputs: Hello, John!

4. Arrow Functions (PHP 7.4+)

  • A shorter syntax for anonymous functions, introduced in PHP 7.4. They are ideal for simple, one-liner functions.
$square = fn($n) => $n * $n;
echo $square(4); // Outputs: 16

5. Recursive Functions

  • Functions that call themselves within their own code to solve problems that can be divided into similar sub-problems, like calculating factorials or traversing tree structures.

function factorial($n) { if ($n <= 1) { return 1; } return $n * factorial($n - 1); } echo factorial(5); // Outputs: 120

6. Parameter and Return Type Declaration Functions

  • You can specify the types of arguments (parameters) that a function accepts, and the type of value it should return. This adds type safety to your code.
function addNumbers(int $a, int $b): int {
return $a + $b; } echo addNumbers(5, 10); // Outputs: 15

7. Variable Functions

  • PHP allows you to call a function using a variable that contains the function's name. This feature is called variable functions.
function greet() {
echo "Hello!"; } $func = 'greet'; $func(); // Outputs: Hello!

8. Magic Functions

  • Special functions in PHP that begin with double underscores (__). These functions are automatically called in certain situations.
  • Examples include:
    • __construct(): Called when a new object is instantiated.
    • __destruct(): Called when an object is destroyed.
    • __call(): Handles calls to undefined methods.
    • __get(): Handles reading from inaccessible properties.

    class MyClass { public function __construct() { echo "Object created!"; } } $obj = new MyClass(); // Outputs: Object created!

9. Static Functions

  • These functions are declared with the static keyword, and they can be called without instantiating the class.
class MathOperations {
public static function add($a, $b) { return $a + $b; } } echo MathOperations::add(5, 3); // Outputs: 8

10. Abstract Functions

  • Abstract functions are declared in abstract classes and do not contain a body. They must be implemented by any class that extends the abstract class.
abstract class Shape {
abstract public function area(); } class Circle extends Shape { public function area() { return 3.14 * 10 * 10; // Example of a circle area } }

11. Final Functions

  • A function declared as final cannot be overridden by subclasses.
class BaseClass {
final public function sayHello() { echo "Hello from BaseClass"; } } class ChildClass extends BaseClass { // Cannot override sayHello() here }

Summary:

  • User-Defined Functions: Custom functions created by the developer.
  • Built-in Functions: Predefined PHP functions.
  • Anonymous Functions: Functions without a name, often used as callbacks.
  • Arrow Functions: Short syntax for simple anonymous functions.
  • Recursive Functions: Functions that call themselves.
  • Variable Functions: Function calls using variables.
  • Magic Functions: Special functions triggered by specific operations.
  • Static Functions: Class methods callable without instantiating the class.
  • Abstract Functions: Functions that must be implemented by subclasses.
  • Final Functions: Functions that cannot be overridden.




















Post a Comment

0 Comments

Visual Studio