PHP programs to pass an object of class as an argument

Here, we are going to learn how to pass an object of class as an argument in PHP?
Submitted by Nidhi, on November 21, 2020

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

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 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");



Id : 101
Name: Rahul


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.

PHP Class & Object Programs »



Comments and Discussions



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

© some rights reserved.