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 where I provided a comprehensive explanation of how OOP works.
Now, let's focus on today's grand adventure, where we'll cover:
- Declaring classes and naming conventions
- Properties and methods with encapsulation
- How to declare constructors
- 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:
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, 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
:
class DatabaseHandler {
}
Properties
Now, let's declare the properties. We need DSN, database user, and database password:
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:
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
:
public function __construct($dsn, $username, $password) {
}
Now, let's add some content to our constructor:
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, this, this, and this 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:
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
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:
class Tom extends Person
To create a child class, we use the extends
keyword. The recipe for a child class is:
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 and other articles there
See you next time
Top comments (0)