It is better (when working with OO style) to break things down in to there own functions in side a class.
A class should do one thing properly. It can have as many moving parts as you want (methods). You can have your class call its own methods.
Your documents class does not seem to do much at all, it checks that the title and the document themselves are not empty, but lets stay with it and look at a few of the options you could take into consideration.
Perhaps it would help to think about how you’d like to initiate and call the class methods from user land:
<?php
// presumably this is coming from a HTTP request ...
$document = "Incoming data forming the document"';
$title = "The document Title";
// $pdo this has been instantiated somewhere else, and is perhaps included
a) You could do this:
// go ahead and instantiate your class
$storer = new DocumentStorer($pdo);
if( $storer->checkNotNull($title) && $storer->checkNotNull($document) ){
$storer->save( $document, $title);
}else{
$storer->getMessages();
}
OR b) you could design it to react to this:
// go ahead and instantiate your class
$storer = new DocumentStorer($pdo);
if( $storer->save($document, $title) === false ){
echo $storer->getMessages();
}
One difference is that in a) you leave the option open to store a document without a Title, or, in the future you could add a new field, Author, without having to go back and change the insides of your DocumentStorer by just doing $storer->checkNotNull($author);
Some might thing this is academic, e.g. why would you even want to instantiate a class when a simple empty() check on the incoming variables prior to starting, but we will leave that aside for the sake of illustrating my points.
Now you could also do c) which is what I think you are trying to do, the big single bang.
// feed the class everything it needs in one go
$storer = new DocumentStorer($pdo, $document, $title);
// I don't much care how you do it, either be successful or get an error message back
if( $storer->save() === false ){
echo $storer->getMessages();
}
How you would build your class would then depend on whether you want to do a) b) or c) (or d, e, f for that matter).
The code to satisfy c) would look something like this, I put some echo statements in there so you can see what is happening, you can add getMessages() or a getErrors() method yourself.
class DocumentStorer {
public $pdo, $title, $doc;
public $errors = array();
function __construct($pdo, $doc, $title){
$this->pdo = $pdo;
$this->title = $title;
$this->doc = $doc;
}
function save(){
echo 'doing save attempt.... ';
$this->checkNotNull($this->doc, 'Document');
$this->checkNotNull($this->title, 'Title');
if( count($this->errors) > 0) {
echo 'Fail ...';
return false;
}else{
// this is where you'd save your data or delegate its storing
echo "Saving $this->doc with $this->title using $this->pdo";
return true;
}
}
function checkNotNull($var, $type){
echo 'doing not null check .... ';
if( strlen(trim($var)) === 0) {
$this->errors[] = "$type has no contents!";
}
}
}
$doc = "this is a doc";
$title = "this is a title";
$pdo = "PDO instance";
$storer = new DocumentStorer($pdo, $doc, $title);
if( !$storer->save() )
var_dump( $storer->errors);