Hi there! Welcome back to another exciting part of our PHP journey.
As in previous article we’ve learned a little about OOP theory - today it’s time for practice
So, what do we do today?
- Declaring classes and naming conventions
- Properties and methods with encapsulation
- How to declare constructors
- Inheritance
But not only theory - we’ll be creating database handler, you can use later on
No more talking - off we go.
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-accepted naming convention for classes.
So, remember, class names always start with a capital letter.
Now, let’s dive into the details.
Properties and Methods
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.
Let’s start creating our database connection class. Create a file called db-class.php
and declare the DatabaseHandler
class:
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 somewhere 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
(with 2 underscores at the beginning):
public function __construct($dsn, $username, $password) {
}
Now, let’s add some content there:
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);
}
// This is where you write your method
}
Inheritance
Now, let’s shift our focus to inheritance. We won’t be needing it in our handler - so let’s copy example from part 14
Now, we’ll talk about code:
<?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 that’s it - I strongly encourage to experiment with code and do something cool with it
Conclusion
To be fair, I thought I wouldn’t finish this article on time - but there it is!
Check out my blog and other articles there See you next time