Last active
February 7, 2019 17:52
-
-
Save brzuchal/a85c7209c0a43bb647e01c03f43dba90 to your computer and use it in GitHub Desktop.
Testing __set against Typed Properties PHP7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string(10) "created_at" | |
string(10) "2019-02-05" | |
object(Message)#1 (5) { | |
["id"]=> | |
int(999) | |
["title"]=> | |
string(12) "Hello World!" | |
["message"]=> | |
string(23) "Here is your message..." | |
["time"]=> | |
int(1549560369) | |
["price"]=> | |
float(1.1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Message { | |
public $id; | |
public $title; | |
public $message; | |
public $time; | |
public $price; | |
public function __set($name, $value) { | |
var_dump($name, $value); | |
} | |
} | |
$message = new Message(); | |
$message->id = 999; | |
$message->title = 'Hello World!'; | |
$message->message = 'Here is your message...'; | |
$message->time = time(); | |
$message->price = 1.1; | |
$message->created_at = '2019-02-05'; | |
var_dump($message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Set default timezone | |
date_default_timezone_set('UTC'); | |
class Message { | |
public int $id; | |
public string $title; | |
public string $message; | |
public int $time; | |
public float $price; | |
public function __set($name, $value) { | |
var_dump($name, $value); | |
} | |
} | |
try { | |
// $db = new PDO('sqlite:messaging.sqlite3'); | |
$db = new PDO('sqlite::memory:'); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$db->exec("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, title TEXT, message TEXT, time INTEGER, created_at DATETIME)"); | |
$messages = [ | |
['title' => 'Hello!', 'message' => 'Just testing...', 'time' => 1327301464], | |
['title' => 'Hello again!', 'message' => 'More testing...', 'time' => 1339428612], | |
['title' => 'Hi!', 'message' => 'SQLite3 is cool...', 'time' => 1327214268], | |
]; | |
$insert = "INSERT INTO messages (title, message, time, created_at) VALUES (:title, :message, :time, date('now'))"; | |
$stmt = $db->prepare($insert); | |
foreach ($messages as $message) { | |
$stmt->bindParam(':title', $message['title']); | |
$stmt->bindParam(':message', $message['message']); | |
$stmt->bindParam(':time', $message['time']); | |
$stmt->execute(); | |
} | |
$stmt = $db->prepare('SELECT * FROM messages'); | |
$stmt->execute(); | |
var_dump($stmt->fetchObject(Message::class)); | |
} catch(PDOException $e) { | |
echo $e->getMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string(2) "id" | |
int(999) | |
string(5) "title" | |
string(12) "Hello World!" | |
string(7) "message" | |
string(23) "Here is your message..." | |
string(4) "time" | |
int(1549560397) | |
string(5) "price" | |
float(1.1) | |
string(10) "created_at" | |
string(10) "2019-02-05" | |
object(Message)#1 (0) { | |
["id"]=> | |
uninitialized(int) | |
["title"]=> | |
uninitialized(string) | |
["message"]=> | |
uninitialized(string) | |
["time"]=> | |
uninitialized(int) | |
["price"]=> | |
uninitialized(float) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Message { | |
public int $id; | |
public string $title; | |
public string $message; | |
public int $time; | |
public float $price; | |
public function __set($name, $value) { | |
var_dump($name, $value); | |
} | |
} | |
$message = new Message(); | |
$message->id = 999; | |
$message->title = 'Hello World!'; | |
$message->message = 'Here is your message...'; | |
$message->time = time(); | |
$message->price = 1.1; | |
$message->created_at = '2019-02-05'; | |
var_dump($message); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment