PROGRAMMING

  MINTE9
REMEMBERS




Last update:   20-02-2022

Parentheses

Type casting in PHP works much as it does in C.
 
/**
 * The name of the desired type is written ...
 * in parentheses before the variable.
 */

$A = array(1, 2, 3);
$B = (string) $A;

var_dump($A); // array(3) { ... }
var_dump($B); // string(5) "Array"

Object

You can call an array item like object property if you type cast array to object.
 
/**
 * Array to Object type casting
 */

$arr = array('senderId' => 10);

$obj = (object) $arr;

echo $obj->senderId; // 10

stdClass

In PHP the default object is of stdClass type..
 
/**
 * stdClass is the default PHP object.
 * stdClass has no properties, methods or parent.
 *
 * It does not support magic methods.
 * It implements no interfaces.
 * 
 * When you cast a scalar or array as Object, ...
 * you get an instance of stdClass.
 */

$obj = (object) array(); // OR
$obj = new stdClass();

$obj->key1 = "value1";
$obj->key2 = "value2";

var_dump($obj);

/*
object(stdClass)[1]
      public 'key1' => string 'value1' (length=6)
      public 'key2' => string 'value2' (length=6)
*/

Questions     github Variables
Aritmetic
        A B C D E F
🔔
1/3