Dhananjay Kuber

Day 8 at rtCamp

Traits

  • Traits are used to declare methods and properties which can be used in multiple class
  • In traits we can have static as method well
  • Traits can only have methods not properties
<?php

trait MyTrait {
  function sayHello() {
    return "Hello";
  }
}

class MyClass {
  use MyTrait;
}

$obj = new MyClass();
echo $obj->sayHello();

?>

Static Keyword

  • Static keyword is used to create static properties and methods which can be called without object of the class
  • We use class name along with :: to access the static method of class
<?php

class MyClass {
  static $staticVariable  = "Static Variable";

  static function myFunc() {
    echo "Static function";
  }
}

echo MyClass::$staticVariable;
MyClass::myFunc();

?>

Namespace

  • Namespace is that groups related classes.
  • Namespaces provide you with a way to group related classes and help you avoid any potential name collisions.
├── index.php
└── src
    ├── Database
    │   └── Logger.php
    ├── Model
    │   ├── Customer.php
    │   └── Product.php
    └── Utils
        └── Logger.php
namespace Store\Database;

class Logger {

}
namespace Store\Model;

class Product {
  
}

class Customer {

}
namespace Store\Utils;

class Logger {
  
}
  • index.php
require 'src/Utils/Logger.php';
require 'src/Database/Logger.php';

use Store\Utils;
use Store\Database;

$loggers = [
    new Utils\Logger(),
    new Database\Logger()
];
  • Here we have 2 Logger class with same name Logger using namespace we have resolved naming collision.
  • We can use the Logger class from both file seperately using use Store\Utils for Utils Logger class and use Utils\Database for Database Logger class
  • We can also use alises for class use Store\Database\Logger as DatabaseLogger; here Logger class from Database namespace
  • To use global classes such as built in classes or user defined classes without a namespace, you need to precede the name of such classes with a backslash \ like \DateTime().

AJAX

  • AJAX stands for Asynchronous Javascript and XML
  • It the used to load the content from server without reloading web page
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    console.log(this.responseText);
  }
};

xmlhttp.open("GET", "getUserName.php", true);
xmlhttp.send();

Mysql

  • In PHP we have 3 approach to communicate with database
    • Mysql Object Oriented Programming
    • Mysql Procedural Programming
    • PDO (PHP Object Data)
  • Each above comes with some benefits but the PDO has something special in it.
  • If we need to migrate to other database (previously on mysql) then we need to changes all the queries. But if we are using the PDO there is no need to change the queries
  • PDO is something like ORM (eg. Prisma)

Database Operations

  • Connection to database
$conn = new mysqli(hostname, username, password, database_name, port);
  • Execute query
$conn->query("SELECT * FROM users");
  • Prepered Statements
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("s", $userId);
$stmt->execute();
$stmt->close();
  • Close Connection
$conn->close();
  • Select Data
$res = $conn->query("SELECT * FROM users");

if($res->num_rows > 0) {
  while(true) {
    $row = $res->fetch_assoc();

    if($row === null) {
      break;
    }

    echo $row['name'];
  }
}