Skip to content

Instantly share code, notes, and snippets.

@gorkamu
Created December 25, 2016 11:43
Show Gist options
  • Save gorkamu/9a0a14ead73179730cf295c65c963e3d to your computer and use it in GitHub Desktop.
Save gorkamu/9a0a14ead73179730cf295c65c963e3d to your computer and use it in GitHub Desktop.
Ejemplo de los métodos mágicos __sleep() y __wakeup()
<?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