If you want to get started with object-oriented programming using PHP, methods are a central component that you should master. In this tutorial, we'll look at how to declare methods and use them effectively in your classes.
Main insights
- Methods are like functions within a class.
- You can use parameters to pass data to methods.
- Return values allow you to use results from methods.
- The visibility of methods (public, private, protected) controls access.
Step-by-step guide
The basic structure of a method
Let's start with the basic declaration of a method. Make sure to create a class that contains the method. For example, your class could be called "Methods". In this class, you can declare the method "add" that adds two numbers. Here is a sample structure:

For the method, you declare it as follows:
The logic behind the addition is stored in the method body.
Within the method body, implement the logic for the addition that sums the two parameters.
The use of echo
To display the result of the addition, you can use the echo function. You want to ensure that the result is easily readable. Combine your text output with the values you are adding.

A simple example looks like this:
This outputs the sum of the two numbers and makes it clear and concise for the user.
Method call
A method does not execute automatically. You need to explicitly make the method call. First, create an object of the Methods class:

Now you can use the "add" method by accessing it through the object:
Output in the browser
To display the result in the browser, call the method and let it return the result. This way, you can see the output directly. Here is a complete example:

Visibility of methods
An important concept in object-oriented programming is visibility. You can declare methods as public, private, or protected. These visibilities control how other parts of your code can access the method.

- public: The method is accessible from anywhere.
- private: The method can only be used within the class.
- protected: The method can be used within the class and by derived classes.
This is particularly important for encapsulating your logic and protecting your data.
Summary – Methods in PHP explained clearly
In this guide, you have learned how methods work in PHP. You now know how to declare them, call them, work with parameters, and return results. You also gained insights into the visibility of methods, which is an important aspect of object-oriented programming.
Frequently Asked Questions
How do I declare a method in PHP?Use the keyword function, followed by the method name and parentheses for parameters.
How do I call a method?Create an object of the class and use the dot operator to call the method.
What are parameters in methods?Parameters are variables that you pass to a method to extend its functionality.
What is the difference between public, private, and protected?public is for everyone, private is only for the class itself, protected is for the class and its subclasses.
How do I return results from a method?Use the keyword return, followed by the value you want to return.