Q:

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

0

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.

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();
?>

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();
?>

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();
?>

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();
?>

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

All Answers

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

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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

Answer 5:

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.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now