2

I'm trying to learn OOP in PHP. What stumps me is understanding just WHAT AN OBJECT IS and how to tell when I'm looking at one. I'm probably very wrong, but here's how I seem to be getting it so far.

Properties are variables inside a class/method. Methods are functions inside a class.

And objects are..... well..... That's exactly where I'm lost. But, as far as I get it (IF I'M GETTING IT), an object is the combination of those properties and methods

So, if

class person {

    var $name;

    function set_name($new_name) {
        $this->name = $new_name;
    }

    function get_name() {
        return $this->name;
    }
}

The class is person the property is $name the method is set_name()

AND WILL THE OBJECT BE SOMETHING LIKE

$person_id = new person();?

Now, if I got EVERYTHING wrong, lol.

2
  • what do you mean what is an object? an object basically is an instance of a class Commented Oct 5, 2014 at 6:30
  • @Ghost Well, I was reading a couple of ebooks and every now and then the word object would pop up and they describe it in such a technical manner that I don't find very clear. Your description of it however as being an instance of a class is so simple I don't need to ask a second time. Thanks! Commented Oct 5, 2014 at 11:37

2 Answers 2

3

I think you've got it exactly right. An object is an instance of a class.

In your case, yes, $person_id is the object. It is a single instance of the class person. It has all the characteristic of the class - its properties and methods, but it uses them independent of other instances of the class. Also note, $person_id is a misleading name. It does not only store the person id, even it you added id as a property. The object is not just the person's id, it is the person - it is the combination (as you said) of all the characteristics and functions that makes up a member of your class, person.

Consider

$person1 = new person();
$person2 = new person();

At this point, both variables are objects - instances of the class person - and they both have the same default characteristics of the class.

Then give them names

$person1->set_name('Steve');
$person2->set_name('Ned');

Now each object has been modified and holds unique information about itself. They retain the same characteristics, or structure, of person, but they are unique and independent of each other.

Think of an object just as the name implies. It is a thing, an object; it stores information about itself that is unique to itself, with a structure defined by its class. Other objects of this class will have the same structure, the same characteristics and inner workings, but they are separate and distinct things, separate and distinct objects that exist, act, and evolve independently.

Sign up to request clarification or add additional context in comments.

1 Comment

Really glad I asked, now things are a lot clearer in my head. Thanks!
0

Basically an object is the instance of the class..

So here $person_id is the object and with this object you can call the methods you defined inside the class person.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.