Friday , 17 November 2017
Home / Programming / PHP / Properties and Methods – PHP OOP Tutorial
Properties and Methods
Properties and Methods

Properties and Methods – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

Now, that we’ve covered some of the basics about object oriented programming, let’s move onto the next step. We’re going to learn about properties and methods. We’ll learn about what they are and how to use them.

Using Properties and Methods

Open up your pet.class.php file and update our class to this.

We create a variable named petName. We then add the keyword public before it. We haven’t gotten to encapsulation yet, but for now understand that this makes our variable accessible outside of our class. You’ll see what I mean in a moment.

In our index.php file let’s try changing this variable. Add this line of code.

Alright, let me break this down for you. If we want to access the variable in our class, we first use the variable that contains the instance of this class. We then use the object operator, ->, to specify that we want to grab something inside this class. We then type in the name of the variable we want to grab. You’ll notice that you don’t have to type $petName, but instead petName. The $ is omitted when you’re trying to access a variable inside of a class. Lastly, we set the value to the string Jack.

Since we defined our variable $petName as public, that means we can access and change that variable inside or outside our class.

A variable inside a class is known as a property. Whenever you define a variable inside a class, you should always refer to it as a property instead of a variable because that’s the proper term for it. There’s no real difference between the 2 besides that variables are defined outside of a class and properties are defined inside a class.

It’s actually not good practice to access and update our properties this way. So, let’s create some functions to do it for us. Inside your class, add these 2 functions.

You’ll notice we have a property named $this. The $this property is a property that points to our class. It’s basically a way to reference itself. You don’t have to define it as PHP define it for you. Why would you do this? Well, let’s update our setPetName() function to this for a second.

As you can see, we have 2 variables named $petName. One is used as a parameter in our function and another is defined in our class. To tell the difference, we use the $this property to reference the one we defined in our class. Of course, you should always try to keep your property names unique.  You should always use the $this variable when referencing a method or a property in your class.

Which brings me to the next topic. What are methods? Methods is just another name for functions inside a class. It’s the proper name for whenever you define a function inside your class. You’ll also notice I typed public in front of the function keyword to let PHP know this function can be accessed outside of the class.  Change your setPetName() function back to it’s original form.

Let’s add some code into the index.php file.

We’re using the same format that we used to access a variable to access a function. The only difference is that when you’re accessing a function, you have to add parenthesis at the end to tell PHP you want a method and not a property.

Conclusion

To summarize it, methods are functions and properties are variables.

About Jasko Koyn

Check Also

Abstract Classes

Abstract Classes – PHP OOP Tutorial

Today, we’ll be discussing what abstract classes are and how we could use them. There …

3 comments

  1. Hello – nice tutorial, but I am confused by the following paragraph:

    “It’s actually not good practice to access and update our properties this way. So, let’s create some functions to do it for us. Inside your class, add these 2 functions.”

    You didn’t appear to access anything in the paragraph or paragraph before so it is unclear as to what you are specifically stating as there is no apparent referred to example. Could you please specify, that would be greatly appreciated.

    thank you

    • He mean that doing ( $jack->petName = “Jack”; ) is not good instead use the method to update and get it
      ( $jack->setPetName(“Giant”); ) <- update
  2. very good tutorial series

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link