[go: up one dir, main page]

0% found this document useful (0 votes)
3 views11 pages

PBLPHP

The document contains multiple PHP scripts demonstrating inheritance and object-oriented programming concepts. It includes examples of single inheritance with classes for Science, Maths, Teachers, and Officers, as well as multiple inheritance using traits for Test and Sports. Additionally, it showcases class creation for Student and College, illustrating how to accept and display data for each instance.

Uploaded by

varaddhadave01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

PBLPHP

The document contains multiple PHP scripts demonstrating inheritance and object-oriented programming concepts. It includes examples of single inheritance with classes for Science, Maths, Teachers, and Officers, as well as multiple inheritance using traits for Test and Sports. Additionally, it showcases class creation for Student and College, illustrating how to accept and display data for each instance.

Uploaded by

varaddhadave01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1) Write a php script to implement following inheritance: Refer Fig.

No.Accept and display total of one object of result.

<?php

// Base class 1: Science

class Science {

protected $phys;

protected $chem;

public function __construct($phys, $chem) {

$this->phys = $phys; // Physics marks

$this->chem = $chem; // Chemistry marks

// Base class 2: Maths

class Maths {

protected $geo;

protected $algo;

public function __construct($geo, $algo) {

$this->geo = $geo; // Geography marks


$this->algo = $algo; // Algorithm marks

// Derived class: Result (inherits from both Science and Maths)

class Result extends Science {

private $geo;

private $algo;

public function __construct($phys, $chem, $geo, $algo) {

// Initialize both parent classes

parent::__construct($phys, $chem);

$this->geo = $geo;

$this->algo = $algo;

// Method to calculate total marks

public function totalMarks() {

return $this->phys + $this->chem + $this->geo + $this->algo;

// Example usage

$student = new Result(85, 90, 80, 75); // Pass marks for each subject

// Output the total marks

echo "Total marks: " . $student->totalMarks();


?>

2) Write a php script to implement inheritance as shown in figure No. 2.


Assume suitable member function. Accept and display data of one
Teacher and one Officer

<?php

// Base class: Staff

class Staff {

protected $code;

public function __construct($code) {

$this->code = $code; // Staff code

}
// Derived class 1: Teacher

class Teacher extends Staff {

private $subject;

public function __construct($code, $subject) {

parent::__construct($code); // Call the parent constructor

$this->subject = $subject; // Subject taught by the teacher

// Method to get teacher details

public function getDetails() {

return "Teacher Code: " . $this->code . ", Subject: " . $this->subject;

// Derived class 2: Officer

class Officer extends Staff {

private $post;

public function __construct($code, $post) {

parent::__construct($code); // Call the parent constructor

$this->post = $post; // Post held by the officer

// Method to get officer details

public function getDetails() {


return "Officer Code: " . $this->code . ", Post: " . $this->post;

// Example usage

$teacher = new Teacher(101, "Mathematics");

$officer = new Officer(102, "Administrator");

// Output the details

echo $teacher->getDetails() . "<br>";

echo $officer->getDetails() . "<br>";

?>

3) Write a php script program to implement multiple inheritance as shown in


Figure . Accept and display data of test marks and sport’s marks using object
of class Result.
<?php

// Trait for Test Class (first base class)

trait Test {

public $marks1;

public $marks2;

public function __construct($marks1, $marks2) {

$this->marks1 = $marks1;

$this->marks2 = $marks2;

// Trait for Sports Class (second base class)

trait Sports {

public $sports_marks;

public function __construct($sports_marks) {

$this->sports_marks = $sports_marks;
}

// Derived Class: Result (using multiple traits for Test and Sports)

class Result {

use Test, Sports;

public $total;

public function __construct($marks1, $marks2, $sports_marks) {

// Initialize base class properties using constructors from traits

$this->marks1 = $marks1;

$this->marks2 = $marks2;

$this->sports_marks = $sports_marks;

// Calculate total marks

$this->total = $this->marks1 + $this->marks2 + $this->sports_marks;

// Method to display result

public function displayResult() {

echo "Marks 1: " . $this->marks1 . "<br>";

echo "Marks 2: " . $this->marks2 . "<br>";

echo "Sports Marks: " . $this->sports_marks . "<br>";

echo "Total Marks: " . $this->total . "<br>";

}
// Example usage

$studentResult = new Result(85, 90, 80); // Example marks for Test and Sports

$studentResult->displayResult(); // Display the result

?>

4) Write a php script program to declare a class student with properties as


roll no and name. Declare a constructor to initialize data members of class.
Display the data.
<?php

// Class definition

class Student {

// Data members

public $roll_no;

public $name;

// Constructor to initialize data members

public function __construct($roll_no, $name) {

$this->roll_no = $roll_no;

$this->name = $name;

}
// Method to display the data

public function display() {

echo "Roll No: " . $this->roll_no . "<br>";

echo "Name: " . $this->name . "<br>";

// Creating an object of the Student class

$student = new Student(101, "John Doe"); // Initialize with roll number 101 and name "John Doe"

// Display the student details

$student->display();

?>

5) Write a php script to declare a class college with name and code. Derive a
new class as student with members or properties as name. Accept and
display details of one student along with college data.

<?php

// Base class: College

class College {
// Data members for College class

public $collegeName;

public $collegeCode;

// Constructor to initialize college details

public function __construct($collegeName, $collegeCode) {

$this->collegeName = $collegeName;

$this->collegeCode = $collegeCode;

// Method to display college details

public function displayCollegeDetails() {

echo "College Name: " . $this->collegeName . "<br>";

echo "College Code: " . $this->collegeCode . "<br>";

// Derived class: Student

class Student extends College {

// Data member for Student class

public $studentName;

// Constructor to initialize student details and call parent constructor

public function __construct($collegeName, $collegeCode, $studentName) {

parent::__construct($collegeName, $collegeCode); // Initialize college details using parent


constructor

$this->studentName = $studentName;

}
// Method to display student details along with college details

public function displayStudentDetails() {

echo "Student Name: " . $this->studentName . "<br>";

// Call the method from the parent class to display college details

$this->displayCollegeDetails();

// Accepting input from the user

$collegeName = "ABC University";

$collegeCode = "1234";

$studentName = "John Doe";

// Create an object of the Student class

$student = new Student($collegeName, $collegeCode, $studentName);

// Display the student details along with college details

$student->displayStudentDetails();

?>

You might also like