1

I was always taught and used $con->prepare for prepared statements. However, this article two answers posted people where using $con->sqli->prepare. I also saw a few others using it in other articles.

Is this something to be concerned with? What is the difference?

  • 1
    They've written their own class that wraps PDO or mysqli, and `$this->con` contains the connection object. – Barmar Mar 04 '18 at 06:57
  • 1
    it should be `$this->con`, not `$this->$con`. You don't put a `$` before the property name (unless you're using a variable property). – Barmar Mar 04 '18 at 06:58
  • This is object orienteted language. Please refere here: https://secure.php.net/language.oop5 – Lithilion Mar 04 '18 at 07:05
  • 1
    This is not the same as the “ducplicated” article. The question is about the extra ->sqli arrow not $this (which in this case is just thnot connection. Thr guy below posted the correct answer. I will mark it as correct. –  Mar 05 '18 at 00:23

1 Answers1

1

Usually some people make database connection class with constructor of connection. It means that when you initialize the object of that class, the constructor is executed automatically.
For example here is Database class

<?php

class db
{
    public $conn;
    public function __construct()
    {
        $this->conn=mysqli_connect("localhost","root","","prepared");//A constructor is a function that is executed after the object has been initialized (its memory allocated, instance properties copied etc.). Its purpose is to put the object in a valid state.
        if($this->conn)
        {
            echo "";
        }
        else{
            echo $this->conn->error;
        }
    }
}
$db = new db();

?>


Child class

include("db.php");

 class childclass extends db
{




public function database_query()//here you don't need to put $conn in parameters
 {
    $sql = "SELECT * FROM table";
    $result = $this->conn->query($sql);//Here you can see how we can call conn from db class
    print_r($result);





 }
}

I hope you got my point.

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42