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

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

Program/Source Code:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.