2. The Barlocker
● Chief Architect at Lucid
Software Inc
● Started using PHP in 2005
● Graduated with BS in CS from
BYU in 2008
● Developed software for the
following industries:
– Network Security
– Social Gaming
– Financial
– Productivity
3. Magic Methods
● All object methods beginning with '__' are reserved.
● To gain the magic functionality, define the method on the
class.
<?php
class MyClass {
private $var1 = 5;
public function doSomething() {
echo "hello!n";
}
}
<?php
class MyClass {
private $var1 = 5;
public function doSomething() {
echo "hello!n";
}
public function __toString() {
return 'This is a MyClass';
}
}
6. __toString
● public string __toString()
● Called when an object is cast as a string.
● Throwing an exception inside this method will cause a
fatal error.
● No default implementation.
7. __toString
<?php
class NoTostringExample {
public $a = 1;
}
$obj = new NoTostringExample();
echo "Stringified " . $obj . "n";
?>
$ php notostring.php
PHP Catchable fatal error: Object of class NoTostringExample could not be
converted to string in notostring.php on line 8
8. __toString
<?php
class TostringExample {
public $a = 2;
public function __toString() {
return 'TostringExample(' . $this->a . ')';
}
}
$obj = new TostringExample();
echo "Stringified " . $obj . "n";
?>
$ php tostring.php
Stringified TostringExample(2)
9. __toString
<?php
class BadTostringExample {
public $a = 3;
public function __toString() {
echo 'TostringExample(' . $this->a . ')' . "n";
}
}
$obj = new BadTostringExample();
echo "Stringified " . $obj . "n";
?>
$ php badtostring.php
TostringExample(3)
PHP Catchable fatal error: Method BadTostringExample::__toString() must return a
string value in badtostring.php on line 11
11. __construct
● public void __construct($params, …)
● Called when an object is first initialized.
● Must explicitly call parent::__construct() in children.
● Default implementation does nothing.
12. __destruct
● public void __destruct()
● Called when an object is garbage collected.
● Must explicitly call parent::__destruct() in children.
● Default implementation does nothing.
13. __destruct
● public void __destruct()
● Called when an object is garbage collected.
● Must explicitly call parent::__destruct() in children.
● Default implementation does nothing.
14. __construct / __destruct
$ php construct.php
Starting
Construct
Ending
Destruct
<?php
class Construct {
public function __construct() {
echo "Constructn";
}
public function __destruct() {
echo "Destructn";
}
}
echo "Startingn";
$obj = new Construct();
echo "Endingn";
?>
15. __construct / __destruct
<?php
class MyDB {
private $connection = null;
public function __construct($host, $user, $pass) {
$this->connection = dbconnect($host, $user, $pass);
}
public function __destruct() {
$this->connection->close();
}
}
17. __get
● public mixed __get($name)
● Called when an inaccessible property is read.
● Not called in chains. ($x = $obj->noexist = 5;)
● Default implementation emits a warning and returns null
or emits a fatal error.
● Does not apply to static context.
18. __set
● public void __set($name, $value)
● Called to write a value to an inaccessible property.
● Default implementation adds a public variable to the
class.
● Does not apply to static context.
19. __isset
● public mixed __isset($name)
● Triggered by calling isset() or empty() on inaccessible
properties
● Default implementation checks for existence of property
and ignores visibility.
● Does not apply to static context.
20. __unset
● public mixed __unset($name)
● Triggered by calling unset() on inaccessible properties
● Default implementation removes accessible properties.
● Does not apply to static context.
21. Property Test
See code from
http://www.php.net/manual/en/language.oop5.overloading.php#object.get
27. __sleep
● public array __sleep()
● Called when serialize() is called on the object.
● Returns an array of field names to include in the serialized
version of the object.
28. __wakeup
● public void __wakeup()
● Called when unserialize() is called on the serialized
object.
29. __sleep / __wakeup
See code from
http://www.php.net/manual/en/language.oop5.magic.php#object.sleep
38. Performance
● Each benchmark was run 10 times.
● Each run is shown as a different set of columns in the
graphs.
● Each run is exactly 1 million calls to the item being tested.
46. Lucid Software Inc
● Building the next generation of collaborative web
applications
● VC funded, high growth, profitable
● Graduates from Harvard, MIT, Stanford
● Team has worked at Google, Amazon, Microsoft
https://www.lucidchart.com/jobs