The proper way to use a class' constructor, is to have it set up the class ready to be used. In many instances the constructor doesn't do much beside accept the dependencies (database object, for instance), and saves these for later use.
(Ab)using the constructor as you've done in your first example leads to many difficulties along the line, mostly due to the fact that simply creating an object has additional side-effects beside just creating said object.
In that manner your second example is a lot closer to what object-oriented programming really is, even though you're still not really taking advantage of anything that using a class gives you. In fact, it would have been better off written as purely procedural code.
While I do not know what your code does, I've attempted to make an example taking advantage of features using a class gives you:
/**
* We need a DB connection in case we need to get something from the database.
* This is called a dependency, and we save it in the object at object creation time.
*
* Nothing that causes the object itself to do "work" is supposed to be here, only things
* necessary for actually creating the object in a state where we can _start_ working with it.
*
* @param PDO $db
*/
public function __construct (PDO $db) {
$this->db = $db;
}
/**
* Sets the value for each step. Legal values for $step is between 1 and 3, inclusive.
* Normally we'd have many setters, one for each property we want to change from outside.
* That can be anything from address to price and anything else one can think of.
*
* @param int $step
* @param int|string $value
*
* @throws InvalidArgumentException
* @return void
*/
public function set_value ($step, $value) {
if ($step <= 0 || $step > 3) {
throw new InvalidArgumentException("Step must be between 1 and 3, inclusive.");
}
$this->value[$step] = $value;
}
/**
* This is where the actual processing is done.
* In a normal class there would be several such functions
* each doing one specific thing (creating a new record, saving
* it to the database, validating stuff, etc).
*
* @return void
*/
public function do_processing () {
$this->result = implode(", ", $this->data);
}
/**
* Fetches the result of the class. Normally we have many getters, which return
* one part of the data associated with the object. Such as username, hash, email, etc.
*
* These are often in a 1-1 relationship with setters.
*
* @return string
*/
public function get_result () {
// If we have no calculated result, fetch it from the DB instead.
if (empty($this->result)) {
return $this->db->get_cached_result ();
}
// Returns the newly calculated result.
// Should probably also cache it, to make the above line useful.
return $this->result;
}
}
// A DB interface class, which extends PDO.
$db = new MyClassDB ();
$obj = new MyClass ($db);
$obj->set_value (2, "Two");
$obj->set_value (1, "One");
$obj->set_value (3, "Three");
$obj->do_processing();
echo $obj->get_result();
Please note that this is a pretty trivial class, and doesn't give you a very good picture of how to leverage classes properly. I recommend looking at a more fleshed out class, which you can find in any of the major frameworks.