PHP program to define methods within the class

Here, we are going to learn how to define methods within the class in PHP?
Submitted by Nidhi, on November 10, 2020 [Last updated : March 13, 2023]

PHP - Methods within Class

Here, we will define a class Sample class with two methods within the class.

PHP code to define methods within the class

The source code to define methods is given below. The given program is compiled and executed successfully.

<?php
//PHP program to define methods within the class.
class Sample
{
    //Methods
    function Method1()
    {
        print ("Method1() called" . '<br>');
    }
    function Method2()
    {
        print ("Method2() called" . '<br>');
    }
}

$S = new Sample();

$S->Method1();
$S->Method2();
?>

Output

Method1() called
Method2() called

Explanation

In the above program, we created a class Sample that contains two methods Method1() and Method2(). After that, we created the object "$S" of the Student class and then called both methods that will print appropriate messages on the console screen.

PHP Class & Object Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.