I've been a procedural PHP programmer. For a few months I've been following OO tutorials and watched a lot of videos on this subject. It's 100% clear that the OO method is way more efficient programming, especially because my project sizes are growing.

So what i want learn the way of object oriented thinking.

We all know the car and animal class examples. Although I understand why they use them as examples and why those are objects, it is not clear what the definition of an object is.

What is the definition of an object?

Is the following example a good example of an object?

class Password {
    public function random($strength) {
        // Returns a random password with a specific strength
    public function check($password, $strength) {
        // Returns if the password meets the strength requirements
    public function hash($password) {
        // Return a hash created with PHP's password_hash() function
    public function match($password, $hash) {
        // Returns if the password matches the hash using password_verify()

Update on answer of Greg Burghardt:

So this would be considered as object oriented?

class Password {
    private $_password;

    public function __construct($password) {
        $this->_password = trim($password);

    public function check($strength) {
        // Returns if the password meets the strength requirements

    public function hash() {
        return password_hash($this->_password, PASSWORD_BCRYPT);

    public function match($hash) {
        return password_verify($this->_password, $hash);
  • Why you trim the password? – SandroMarques Jan 9 at 11:03
  • A user can have unwanted spaces in the password field by accident (e.g. when copy pasting which happens often) – Ramon Bakker Jan 9 at 11:40
  • Ok @Ramon Bakker, but what if the user wants a space? You should never change the entered password, such as trimming or limiting the length. – SandroMarques Jan 9 at 20:22
  • @SandroMarques You should not limit passwords by characters or length any further, indeed. I know. Spaces are allowed within the password, only not as start or ending character. Thats all :) (note that Facebook, Google and Microsoft are also trimming start and ending spaces) – Ramon Bakker Jan 9 at 20:53

An object is a combination of state, in the form of data properties, and behavior in the form of methods.

Your example does not appear to contain any state, and therefore is procedural code wrapped in an object.

If you passed in the max length and strength as arguments to the constructor then you would have state. This would be considered object-oriented.

Additionally, what you have created is a Password Generator which is also a better class name.

$generator = new PasswordGenerator(14, 80);
$password = $generator->random();

Seeing your updated code in your answer, yes, the updated code is object oriented since you are keeping track of the password and exposing public methods that operate on that data.

  • Please see the update on my question. – Ramon Bakker Jan 26 '17 at 22:58
  • So, Password and PasswordGenerator would be 2 completely different objects, right? – Ramon Bakker Jan 26 '17 at 23:08
  • Not just different objects, but also different classes. An object is a instance of a class. – Greg Burghardt Jan 26 '17 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.