How to declare a method?
Let's declare a method inside a class named Example class to echo out a simple string that we give.
We use the public keyword to make the method available inside and outside the class. You will learn more about this in the visibility chapter.
How to call a method?
$example = new Example(); $example -> echo('Hello World');
Result: Hello WorldExplained:
- First, we create an object ($example) from the class Example
- Next, we call the method echo with -> (object operator) and () (parentheses)
- The parentheses contain the arguments as usual
The thing you need to understand is that we call methods on objects, not classes.