PHP – the journal

#01 | Smarty – php templating engine
php statements mixed with html markup can get messy. Smarty solves this with simple tag-based syntax. Modern counterparts: ‘handlebars’ in Ember.js

all event owners:
$sql = “SELECT attendee.*,user.email FROM attendee INNER JOIN user ON attendee.U_ID=user.U_ID WHERE attendee.isOwner=1 GROUP BY(U_ID)”;

owner for event:
$sql = “SELECT attendee.*,user.email FROM attendee INNER JOIN user ON attendee.U_ID=user.U_ID WHERE attendee.isOwner=1 AND attendee.E_ID={$evtId}”;

#02 | Session in PHP
each visitor accessing a php website is assigned a unique id (session id). This is either stored in:
1. a cookie on the user side or
2. propagated in the URL

The session support allows you to store data between requests in the $_SESSION superglobal array.

#03 | Database Class & Usage

<?php

class DatabaseHelper  {
  var $host = "localhost";
  var $user = "root";
  var $pass = "root";
  var $db = "contactdb";

  var $connId = 0;
  var $queryId = 0;

  function connect() {
    $this->connId = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
    if (!$this->connId) {
      die('Could not connect to database!');
    }
    return $this->connId;
  }

  function query($query) {
    $this->connect();
    $this->queryId = mysqli_query($this->connId, $query);
    $this->close();

    if(!$this->queryId) {
      $this->halt( "Invalid SQL: $query");
    }
    return $this->queryId;
  }

  function close() {
    mysqli_close($this->myconn);
  }
}
?>

Usage:

<?php
  include('DatabaseHelper.class.php');

  $connection = new DatabaseHelper();

  $contacts = array();
  $sql = "SELECT * FROM member";
  $res = $connection->query($sql);

  while($row = mysqli_fetch_array($res)) {
    array_push($events, array('id'=>$row['id'], 'email'=>$row['email']));
  }
  echo json_encode(array("result"=>$contacts));
?>

Output:

{"result":[{"id":"1","email":"abc@gmail.com"}]}

#05 | Singleton

<?php
/**
 * Singleton class
 */
final class Singleton {
    /**
     * Call this method to get singleton
     * @return UserFactory
     */
    public static function Instance() {
        static $inst = null;
        if ($inst === null) {
            $inst = new Singleton();
        }
        return $inst;
    }

    /**
     * Private consturctor prevents instantiation
     */
    private function __construct() {
    }

    public function hello() {
      echo "<br/>Hello World<br/>";
    }
}

Usage:

<?php
  include('Singleton.class.php');

  $s1 = Singleton::Instance();
  $s2 = Singleton::Instance();

  $isSingleton = ($s1 === $s2) ? "same instance" : "differnet instances";
  echo "<br/>Singleton Test: {$isSingleton} <br/>";

  $s2->hello();
?>

#04 | Reverse a Number

<?php
echo "reversed: ".rev(-4321);

function rev($num) {
  $result = 0;
  while ($num != 0) {
	$result = $result * 10 + $num % 10;
	$num = (int) ($num / 10);
  }
  return $result;
}
?>

#05 | Util: array_sum, strpos, ‘&’

$strNumArry = "5,3,2";
echo array_sum(explode(',',$strNumArry));

$s1 = 'hello world';
$s2 = 'hello';
if (strpos($s1,$s2) !== false) {
    echo "<br/>".$s1." contains, ".$s2;
} else {
    echo "<br/>". $s1." does not contain, ".$s2;
}

$a = '1';$b = &$a;

$a = '22';
echo "<br/><br/>dollar b is only a refernece to dollar a: $b";

#06 | Email

$to_addr = ‘you@yourdomain.com’;

$mail_body = ‘Name: ‘.$_REQUEST[‘name’];
$mail_body .= “\nEmail: “.$_REQUEST[’email’];
$mail_body .= “\nMessage: “.$_REQUEST[‘message’];
mail($to_addr,’Form Feedback’,$mail_body);
print “Thank you for the feedback.”;

#07 | instanceof

if ($obj instanceof ClassA) {
   echo 'ClassA';
}
is_subclass_of($obj, 'ClassSuper');

#08 | interfaces and abstract classes

<?php
abstract class Grandpa {}
interface Father {}
interface Son {}

class Base extends Grandpa implements Father {}
class Child extends Base implements Son {}

$b = new Base();
$c = new Child();

/* All of the following are true. */
var_dump($b instanceof Grandpa);
var_dump($b instanceof Father);

var_dump($c instanceof Son);
var_dump($c instanceof Father);
var_dump($c instanceof Grandpa);

?>

#09 | Git Flow

git clone repo-url.git

# now get new branches from remote
git fetch --all

# checkout a branch you want to see
git checkout master

git pull

# feature branch create & commit
git branch -b feature/my-feature
git add .
git commit -m "message"
git push origin feature/my-feature

#10 | reference
JSON: https://guides.emberjs.com/v2.5.0/tutorial/model-hook/
i18n: https://lingohub.com/blog/2013/06/php-internationalization-i18n-mechanisms-tutorial/

Advertisements