PHP 0 to hero pt.15 - OOP coding
Tags: php, webdev, beginners, tutorial
Published: 2024-02-22 21:22:10

Hi there! Welcome back to another exciting part of our PHP journey. Today, we're delving into the captivating world of object-oriented programming (OOP). If you've just stumbled upon this article and OOP is a bit of a mystery to you, fear not! Check out the [previous part](https://wizarddos.github.io/blog/programming/php_0_to_hero/14) where I provided a comprehensive explanation of how OOP works.

Now, let's focus on today's grand adventure, where we'll cover:

1. Declaring classes and naming conventions
2. Properties and methods with encapsulation
3. How to declare constructors
4. Inheritance

As an illustrative example, we'll create a class for handling database connections in PHP. So, buckle up, and let's get started!

## Classes

To create a class, we use the `class` keyword:

```php
class ClassName {

}
```

Ever wondered why a class name starts with a capital letter? It's the `StudlyCaps` convention, and it's the PSR naming convention for classes. So, remember, class names always start with a capital letter.

Now, let's dive into the details.

## Properties and Methods

I've covered these concepts theoretically in the [previous part](https://wizarddos.github.io/blog/programming/php_0_to_hero/14), so if you missed it, take a moment to catch up.

For the sake of readability, when creating a class, it's common practice to first declare properties and then methods. The first method declared should be the constructor, and the last should be the destructor.

Now, let's start creating our DB connection class. Create a file called `db-class.php` and declare the class `DatabaseHandler`:

```php
class DatabaseHandler {
}
```

### Properties

Now, let's declare the properties. We need DSN, database user, and database password:

```php
private $db_dsn;
private $db_user;
private $db_pass;
protected $db_conn;
```

Since we don't need to access these data from outside of this class, we've made them private. `$db_conn` is protected, as it might be useful later.

Our class now looks like this:

```php
class DatabaseHandler {
    private $db_dsn;
    private $db_user;
    private $db_pass;
    protected $db_conn;
}
```

### Methods

Why create a database handler if it can't handle database connections? Let's fix that. We'll start with a constructor. To declare it, we create a function called `__construct`:

```php
public function __construct($dsn, $username, $password) {

}
```

Now, let's add some content to our constructor:

```php
public function __construct($dsn, $username, $password) {
    $this->db_dsn = $dsn;
    $this->db_user = $username;
    $this->db_pass = $password;
    $this->db_conn = new PDO($dsn, $username, $password);
}
```

That's what the constructor is for – initializing the class. And what's this mysterious `$this`? It's a reference to the current object.

Now, with the help of [this](https://wizarddos.github.io/blog/programming/php_0_to_hero/12), [this](https://wizarddos.github.io/blog/programming/php_0_to_hero/14), [this](https://wizarddos.github.io/blog/programming/php_0_to_hero/13), and [this](https://wizarddos.github.io/blog/programming/php_0_to_hero/9) article, I challenge you to write a method that executes SQL queries and returns their results as associative arrays. In the next article, I'll share my solution with comments on how it works.

That's it! Our class is ready:

```php
class DatabaseHandler {
    private $db_dsn;
    private $db_user;
    private $db_pass;
    protected $db_conn;

    public function __construct($dsn, $username, $password) {
        $this->db_dsn = $dsn;
        $this->db_user = $username;
        $this->db_pass = $password;
        $this->db_conn = new PDO($dsn, $username, $password);
    }

    // Place for your method
}
```

### Inheritance

Now, let's shift our focus to inheritance. In the example below, we'll use the same analogy as last time, but let's focus on the code rather than theory:

```php
<?php

class Person {
    public $name;
    protected $age;
    private $phone;

    public function talk() {
        // Do stuff here
    }

    protected function walk() {
        // Do stuff here
    }

    private function swim() {
        // Do stuff here
    }
}

class Tom extends Person {
    // Tom class extends Person class
    // Tom inherits all public and protected members from Person

    // So class Tom will have these properties and methods:
    // public $name;
    // protected $age;
    // public function talk(){}
    // protected function walk(){}

    // But it will not inherit the private members
    // This is what object inheritance means
}
```

Notice this line:

```php
class Tom extends Person
```

To create a child class, we use the `extends` keyword. The recipe for a child class is:

```php
class ChildName extends ParentName
```

And there you have it! The world of PHP OOP is full of exciting possibilities. Dive in and experiment

## Conclusion

To be fair, I thought I won't finish this article on time - but there it is!

Check out my [blog](https://wizarddos.github.io/blog/) and other articles there
See you next time