Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Class Methods

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $6.00

Arrays and objects have a lot in common: arrays have keys while objects have properties, but both store data.

Objects have one other big, incredible, mind-blowing advantage: methods. A method is a fancy word for a function, and we're all comfortable with those. The difference is that a method lives inside of a class.

To create a method, start with public function. After this, it looks like a normal function - let's call ours sayHello, and add the normal parenthesis and curly braces stuff. Inside, we can do anything - so let's echo Hello:

20 lines play.php
... lines 1 - 2
class Ship
{
public $name;
public function sayHello()
{
echo 'Hello!';
}
}
... lines 12 - 20

To call the method, let's use our object "arrow" syntax. Let's add an <hr/> so I don't confuse myself, then $myShip, then arrow, then sayHello():

20 lines play.php
... lines 1 - 13
$myShip = new Ship();
... lines 15 - 18
$myShip->sayHello();

The only difference between accessing a property and a method is the open parenthesis and close parenthesis, so don't forget that. And a method is just like a traditional function, except it lives inside of the class, so you have to call it on the object. And when we refresh, success!

This is kind of amazing because we have little packages of data that can now perform actions and do things through methods. Arrays can't do anything like that.

Referencing Properties from Inside a Class

So let's add another - a getName() method. Remember how functions can return a value? Sure, methods can do that too. Let's not actually return the name of this ship yet, let's fake it:

25 lines play.php
... lines 1 - 2
class Ship
{
... lines 5 - 11
public function getName()
{
return 'FAKE NAME';
}
}
... lines 17 - 25

Down below, we call it the exact same way, except we can echo what it returns:

25 lines play.php
... lines 1 - 18
$myShip = new Ship();
$myShip->name = 'TIE Fighter';
echo 'Ship Name: '.$myShip->getName();
... lines 23 - 25

Now refresh! There's our fake name. Of course, what I really want to do is return the name of the ship that I'm calling this method on. I know, not the most interesting function, but it gives us a new problem. When we have the $myShip variable, we know how to access a property - just use the arrow syntax on it. But when we're inside of the class, how can we get the value of the name property?

The answer is with a very special variable called $this:

25 lines play.php
... lines 1 - 2
class Ship
{
... lines 5 - 11
public function getName()
{
return $this->name;
}
}
... lines 17 - 25

Here's the rule: when you're inside of a method, you magically have access to a variable called $this. And $this is whatever Ship object that we're calling the method on, in our case our favorite $myShip object whose name is "Jedi Starship". In all cases, the variable is called $this, that's just what the PHP Jedi elders decided this magic name should be.

When we refresh, hey - there's our ship's real name!

Adding more Properties

Our Ship class has just one property. Let's go back and look at the get_ships() function I wrote before we started. Here, each ship has a key for name, weapon_power, jedi_factor and strength. Let's add three more properties to our class: weaponPower, jediPower and strength. I camel-cased weaponPower and jediPower - that's kind of a standard, but you can do whatever you want:

31 lines play.php
... lines 1 - 2
class Ship
{
public $name;
public $weaponPower;
public $jediFactor;
public $strength;
... lines 12 - 21
}
... lines 23 - 31

Default Property Values

You can also give a property a default value. So if you create a new Ship object and we never set the weaponPower, let's default its value to zero. Let's do that for jediFactor and strength too.

31 lines play.php
... lines 1 - 2
class Ship
{
public $name;
public $weaponPower = 0;
public $jediFactor = 0;
public $strength = 0;
... lines 12 - 21
}
... lines 23 - 31

These new properties aren't special, so we can use them like before. Let's var_dump the weaponPower property:

33 lines play.php
... lines 1 - 24
$myShip = new Ship();
... lines 26 - 30
echo '<hr/>';
var_dump($myShip->weaponPower);

When we refresh, it dumps zero. Cool! That's using our default value because we never actually set the weaponPower. Let's set it now - $myShip->weaponPower = 10;:

34 lines play.php
... lines 1 - 24
$myShip = new Ship();
$myShip->name = 'TIE Fighter';
$myShip->weaponPower = 10;
... lines 28 - 31
echo '<hr/>';
var_dump($myShip->weaponPower);

Now we see 10. Awesome!

Leave a comment!

5
Login or Register to join the conversation

Hi,

courses 1 to 3 of OOP seem have "Drupal 8" track in the breadcrumb (below SFCast logo) ;)

Reply

Hey Steven!

Yea, that's a quirk with our track system - this course belongs in 2 tracks (OO & Drupal) and there's a bit of a shortcoming in our system where we can't currently mark a track as a "primary" track. Something we need to fix one of these days :).

Cheers!

Reply
Marc R. Avatar
Marc R. Avatar Marc R. | posted 3 years ago

Hi !
I noticed that since the beginning of the course the name of the ship is sometimes Tie fighter and the other time Jedi starship.
Code and comments don't seem to speak the same thing. It's a little confusing.

Reply

Hey Marc R.

We use different names to make it clear that we're talking about different objects but yes, it's still a bit confusing. We're sorry about that

Reply
Victor Avatar Victor | SFCASTS | posted 5 years ago | edited

Hey @disqus_jfv7blioph ,

You're right, quick catch! Actually, there's a good example :) As you can see, do output inside a method is not a good idea, because it's not obvious. That's why better to return a value inside, and then echo it in the place where the method was called. Or probably, use a good method naming, something like printName() instead of getName(), it'll make more sense. But anyway, returning the value is a better option ;)

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

userVoice