Engineers Work In Their Sleep
Wed, Jul 7, 2010 10:42 AM
<?phpThe above code should generate:
$mc = new Memcached();
$mc->addServer("localhost", 11211);
$mc->set(123, "yes!");
var_dump($mc->getMulti(array(123)));
?>
array(1) {
[123]=>
string(4) "yes!"
}But, in reality, it generates:bool(false)The set succeeds, but the getMulti fails. Again, this is being fixed in GitHub, but is not available for release on PECL.
<?phpNow you can include the publications.php file and have a global variable named $PUBLICATIONS that holds the publication settings. But, how do we load a single publication without knowing numeric ids? Well, you could make some constants.
$sql = "select * from publications";
$res = $mysqli->query($sql);
while($row = $res->fetch_assoc()){
$pubs[$row["publication_id"]] = $row;
}
$pubs_ser = str_replace("'", "\\'", serialize($pubs));
$php_code = "<?php global \$PUBLICATIONS; \$PUBLICATIONS = unserialize('$pubs_ser'); ?>";
file_put_contents("/some/path/publications.php", $php_code);
?>
<?phpSo, now, we have constants. We can do stuff like:
$sql = "select * from publications";
$res = $mysqli->query($sql);
while($row = $res->fetch_assoc()){
$pubs[$row["publication_id"]] = $row;
$constants[$row["publication_id"]] = strtoupper($row["name"]);
}
$pubs_ser = str_replace("'", "\\'", serialize($pubs));
$php_code = "<?php\n";
$php_code.= "global \$PUBLICATIONS;\n";
$php_code.= "\$PUBLICATIONS = unserialize('$pubs_ser');\n";
foreach($constants as $id=>$const){
$php_code.= "define('$const', $id);\n";
}
$php_code.= "?>";
file_put_contents("/some/path/publications.php", $php_code);
?>
<?phpBut, how about autoloading? It would be nice if I could just autoload the constants.
//load a publication
require_once "publications.php";
echo $PUBLICATIONS[DEALNEWS]["name"];
?>
<?phpThen we create a class in our autoloading directory that extends that object.
$sql = "select * from publications";
$res = $mysqli->query($sql);
while($row = $res->fetch_assoc()){
$pubs[$row["publication_id"]] = $row;
$constants[$row["publication_id"]] = strtoupper($row["name"]);
}
$pubs_ser = str_replace("'", "\\'", serialize($pubs));
$php_code = "<?php\n";
$php_code.= "class PUB_DATA {\n";
foreach($constants as $id=>$const){
$php_code.= " const $const = $id;\n";
}
$php_code.= " protected \$pubs_ser = '$pubs_ser';\n";
$php_code.= "}";
$php_code.= "?>";
file_put_contents("/some/path/pub_data.php", $php_code);
?>
<?phpGreat, now we can do things like:
require_once "pub_data.php";
class Publication extends PUB_DATA {
private $pub;
public function __construct($pub_id) {
$pubs = unserialize($this->pubs_ser);
$this->pub = $pubs[$pub_id];
}
public function __get($var) {
if(isset($this->pub[$var])){
return $this->pub[$var];
} else {
// Exception
}
}
}
?>
$pub = new Publication(Publication::DEALNEWS);The only problem that remains is dealing with getting the generated code to all your servers. We use rsync. It works quite well. You may have a different solution for your team. Back when we ran our own in house ad server we did all the ad work this way. None of the ad calls ever hit the database to get ads. We stored stats on disk in logs and processed them on a schedule. It was a very solid solution.
echo $pub->name;
create table clicks_new like clicks;
rename table clicks to clicks_2010032500001, clicks_new to clicks;