Created
December 25, 2016 11:43
-
-
Save gorkamu/9a0a14ead73179730cf295c65c963e3d to your computer and use it in GitHub Desktop.
Ejemplo de los métodos mágicos __sleep() y __wakeup()
This file contains hidden or 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 Connection | |
{ | |
protected $link; | |
private $dsn, $username, $password; | |
public function __construct($dsn, $username, $password) | |
{ | |
$this->dsn = $dsn; | |
$this->username = $username; | |
$this->password = $password; | |
$this->connect(); | |
} | |
private function connect() | |
{ | |
$this->link = new PDO($this->dsn, $this->username, $this->password); | |
} | |
public function __sleep() | |
{ | |
return array('dsn', 'username', 'password'); | |
} | |
public function __wakeup() | |
{ | |
$this->connect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment