ADVERTISEMENT
ADVERTISEMENT

PHP find output programs (Class & Objects) | set 1

Find the output of PHP programs | Class & Objects | Set 1: Enhance the knowledge of PHP Class & Objects by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 28, 2021

Question 1:

<?php
    class Student
    {
        public $id;
        public $name;
        public $fee;
    
        function set($id, $name, $fee)
        {
            $this->id = $id;
            $this->name = $name;
            $this->fee = $fee;
        }
    
        function display()
        {
            echo "<br>ID 	: $this->id";
            echo "<br>NAME  : $this->name	";
            echo "<br>FEE   : $this->fee	";
        }
    }
    
    $obj1 = new Student();
    $obj2 = new Student();
    
    $obj1->set(101, "Rahul", 1000);
    $obj2->set(102, "Rohit", 2000);
    
    echo "Student1:";
    $obj1->display();
    
    echo "<br><br>Student2:";
    $obj2->display();
?>

Output:

Student1:
ID : 101
NAME : Rahul
FEE : 1000

Student2:
ID : 102
NAME : Rohit
FEE : 2000

Explanation:

In the above program, we created a class that contains three data members $id, $name, and $fee, we also created to methods set and display(). The set() method is used to set values into the data members, and display() method is used to display values of data members.

Then we created two objects $obj1 and $obj2, and we set and display values of both objects.

Question 2:

<?php
    class Student
    {
        public $id;
        public $name;
        public $fee;
    
        function set($id, $name, $fee)
        {
            $this->id = $id;
            $this->name = $name;
            $this->fee = $fee;
        }
    
        function print ()
        {
            echo "<br>ID 	: $this->id";
            echo "<br>NAME  : $this->name	";
            echo "<br>FEE   : $this->fee	";
        }
    }
    
    $obj1 = new Student();
    $obj2 = new Student();
    
    $obj1->set(101, "Rahul", 1000);
    $obj2->set(102, "Rohit", 2000);
    
    echo "Student1:";
    $obj1->print();
    
    echo "<br><br>Student2:";
    $obj2->print();
?>

Output:

Student1:
ID : 101
NAME : Rahul
FEE : 1000

Student2:
ID : 102
NAME : Rohit
FEE : 2000

Explanation:

In the above program, we created a class that contains three data members $id, $name, and $fee, we also created to methods set and print(). The set() method is used to set values into the data members, and the print() method is used to display values of data members.

Then we created two objects $obj1 and $obj2, and we set and print values of both objects.

Note: As we know that print() is a built-in function, but here we defined a method with name print. But the scope of both is different, so it will not create any problem.

Question 3:

<?php
    class Student
    {
        public $id;
        public $name;
        public $fee;
    
        function set($i, $n, $f)
        {
            $id = $i;
            $name = $n;
            $fee = $f;
        }
    
        function print ()
        {
            echo "<br>ID 	: $this->id";
            echo "<br>NAME  : $this->name	";
            echo "<br>FEE   : $this->fee	";
        }
    }
    
    $obj1 = new Student();
    $obj2 = new Student();
    
    $obj1->set(101, "Rahul", 1000);
    $obj2->set(102, "Rohit", 2000);
    
    echo "Student1:";
    $obj1->print();
    
    echo "<br><br>Student2:";
    $obj2->print();
?>

Output:

Student1:
ID :
NAME :
FEE :

Student2:
ID :
NAME :
FEE :

Explanation:

In the above program, we created a class that contains three data members $id, $name, and $fee, we also created to methods set and print().

The set() method is used to set values into the data members, but we did not use $this with data members of class inside the set() method, then values will not set.

The print() method is used to display the values of data members.

Then we created two objects $obj1 and $obj2, and we set and print values of both objects. We here empty values will print on the web page because we did not use $this with data members in the set() method.

Question 4:

<?php
    class Student
    {
        public $id;
        public $name;
        public $fee;
    
        function set($id, $name, $fee)
        {
            $this->id = $id;
            $this->name = $name;
            $this->fee = $fee;
        }
    
        function print ()
        {
            echo "<br>ID 	: $this->id";
            echo "<br>NAME  : $this->name	";
            echo "<br>FEE   : $this->fee	";
        }
    }
    
    Student $obj1 = new Student();
    Student $obj2 = new Student();
    
    $obj1->set(101, "Rahul", 1000);
    $obj2->set(102, "Rohit", 2000);
    
    echo "Student1:";
    $obj1->print();
    
    echo "<br><br>Student2:";
    $obj2->print();
?>

Output:

PHP Parse error:  syntax error, unexpected '$obj1' (T_VARIABLE) 
in /home/main.php on line 23

Explanation:

The above program will generate syntax error, because we did not declare objects in proper way. The way of object declaration is given below:

$obj1 = new Student();
$obj2 = new Student();

Question 5:

<?php
    class Sample
    {
        public $val;
    
        function set($v)
        {
            $this->val = $v;
        }
    
        function ADD($B)
        {
            $temp = $this->val + $B->val;
            return $temp;
        }
    }
    
    $obj1 = new Sample();
    $obj2 = new Sample();
    
    $obj1->set(100);
    $obj2->set(200);
    $RES = $obj1->ADD($obj2);
    
    echo "Result: $RES";
?>

Output:

Result: 300

Explanation:

In the above program, we created a class Sample that contains data member $val. Here, we also created two methods set() and ADD().

The set() method is used to set the value of data member $val, and the ADD() method is used to add the value of the specified object with the value of the current object and return the addition of both.



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.