Q:

PHP programs to pass an object of class as an argument

belongs to collection: PHP Classes & Objects Programs

0

Here, we will demonstrate how we can pass an object as an argument to the non-member function?

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 pass an object of class as an argument is given below. The given program is compiled and executed successfully.

<?php
//PHP programs to pass an object of class as an argument.
class Student
{
    public $Id;
    public $Name;
}

function Set(Student $S, $id, $name)
{
    $S->Id = $id;
    $S->Name = $name;
}
function Display(Student $S)
{
    printf("Id  : " . $S->Id . "<br>");
    printf("Name: " . $S->Name . "<br>");
}

$S = new Student();

Set($S, 101, "Rahul");
Display($S);

?>

Output:

Id : 101
Name: Rahul

Explanation:

Here, we create a class Student that contains two data members Id and Name. After that we defined two function Set() and Display(). Here, we passed the object as an argument in both functions.

The Set() function is used to set the values of the data member of class Student, and Display() function is used to print the values of data members on the webpage.

At last, we created object $S of Student class then we called both Set() and Display() functions.

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 programs to demonstrate the function returning... >>
<< PHP program to demonstrate the method overloading ...