<?php
public class Post
{
private function __construct($id)
{
/* Private constructor... do your normal initialization here */
}
private static function isValidID($id)
{
if (/*... check if $id is valid ... */) {
return true;
}
return false;
}
public static function create($id)
{
if (!self::isValidID($id)) {
return NULL; // Or throw an exception
}
return new Post($id);
}
}
?>
And then you would use it, like:
<?php
$post = Post::create(1234);
if (is_null($post)) {
/* Invalid ID */
}
?>
Or if you opted to throw an exception:
<?php
try {
$post = Post::create(1234);
} catch (YourInvalidIDException $e) {
/* Invalid ID */
}
?>