Q:

PHP program to define methods within the class

belongs to collection: PHP Classes & Objects Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Classes & Objects Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP program to initialize data members without usi... >>
<< PHP program to create multiple objects of a class ...