function __construct

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
mactron
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Feb 14, 2020 4:58 am

Fri Feb 14, 2020 5:14 am

Hi! I'm new here :)

I'm following an online course where we building CMS using OOP PHP and PDO. I read some articles and watched a few youtube tutorials and to be honest, I'm pretty confused. Some of the developers suggest using function __construct(PDO $connection) inside the class.

Their code looks something like that:

Code: Select all

private PDO $connection;
  function __construct(PDO $connection) {
    $this->connection = $connection;
Honestly speaking I don't know how to use this function inside my class. Did I miss something? Is my code down below wrong? Please note: The code works like a charm I just don't understand the concept of using function __construct(PDO $connection). Any advice is appreciated! Thanks!

connect.php

Code: Select all

    class dbh{
    
        private $host = "localhost";
        private $user = "root";
        private $pwd = "";
        private $dbname = "cms";

        protected function connect() {
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
            $pdo = new PDO ($dsn, $this->user, $this->pwd);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            return $pdo;

    }
}
function.php // not a whole code

Code: Select all

class CategoriesData extends dbh{

    public function getAllCategories() {

			$sql = "SELECT * FROM categories_tbl";
			$stmt = $this->connect()->query($sql);
				while ($row = $stmt->fetch()){
				
				echo $row['category_name'] . '<br>';

		}
    }

 	public function getCategoryName() {

			$sql = "SELECT category_name FROM categories_tbl";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute();
			$category = $stmt->fetch(PDO::FETCH_OBJ);
			
			if($category == null) {

				return null;
			}else {

				return $category->category_name;
			}
    } 

	public function getCategoryDetials() {

			$sql = "SELECT * FROM categories_tbl";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll((PDO::FETCH_OBJ));
			
			return $result;
    }   
		
    public function addCategory ($filter_name, $filter_title, $filter_description, $filter_slug) {

			$sql = "INSERT INTO categories_tbl (category_name, category_title, category_description, category_slug) VALUES (?, ?, ?, ?)";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$filter_name, $filter_title, $filter_description, $filter_slug]);
 
		}

	public function deleteCategory($delete_category_id) {
			$sql =  "DELETE FROM categories_tbl WHERE category_id = ?";
			$stmt = $this->connect()->prepare($sql);
			$stmt->execute([$delete_category_id]);
    }
    }
       }
   
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 972
Joined: Mon Feb 22, 2016 5:52 pm

Fri Feb 14, 2020 10:18 am

The __construct function allows you to pass parameters to the class when it is instantiated (when it is created, constructed or an instance is made of it.)

As a very simple example, the code below shows the same thing, done it two ways:

Without __construct

Code: Select all

<?php
class myClass {
  private $something = NULL;
  
  public function set_something ($something) {
    $this->something = $something;
  }
  public function get_something(){
    return $this->something;
  }
}

$myVar = new myClass();
$myVar -> set_something('Hello');
echo $myVar->get_something();
with __construct

Code: Select all

<?php
class myClass {
  private $something = NULL;
  
  public function __construct ($something) {
    $this->something = $something;
  }
  public function get_something(){
    return $this->something;
  }
}

$myVar = new myClass('Hello');
echo $myVar->get_something();
The second script saves one line of code, neither is better than the other; sometimes though, one way of doing it will make your code easier to read and therefore easier to edit.

Many people have their own way of coding, and use different methodologies, and often different interpretations of those methodologies. The best way to understand what is going on is to play with the code and see what happens, classes can seem quite daunting until everything clicks into place.
Post Reply