Php Interview Questions
Php Interview Questions

Php Interview Questions

(1)Abstract Class: Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks. An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.

Example:
abstract class Marks{
abstract public function marksDetails($total_mark,$percentage);
}
class Student extends Marks{
public function studentInfo($studentName,$class,$subjects){
echo “Student Name: “.$studentName;
echo “Class: “.$class;
echo “Subject: “.$subjects;
}
public function marksDetails($total_mark,$percentage){
echo “Total Mark: “.$total_mark;
echo “Percentage: “.$percentage;
}
}
$studentName=”Siddhartha Kul”;
$class=”M.C.A”;
$subjects=”All Computer Science”;
$total_mark=”560″;
$percentage=”80%”;
$objStu= new Student();
$objStu->studentInfo($studentName,$class,$subjects);
$objStu->marksDetails($total_mark,$percentage);

(2.)Static Class: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::).
Example:
class Student{
static public function studentInfo($studentName,$class,$subjects){
echo “Student Name: “.$studentName;
echo “Class: “.$class;
echo “Subject: “.$subjects;
}
}
$studentName=”Siddhartha Kul”;
$class=”M.C.A”;
$subjects=”All Computer Science”;
$objStu= new Student();
$objStu->studentInfo($studentName,$class,$subjects);
Student::studentInfo($studentName,$class,$subjects);

(3)Final Class: The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.

Example:
final class mathsOperation1{
public function calculations($input1,$input2){
echo “Multiply: “.$input1*$input2;
}
}
class mathsOperation2 extends mathsOperation1{
public function calculations($input1,$input2){
echo “Addition: “.$input1+$input2;
}
}
$studentName=”21″;
$class=”10”;
$obj= new mathsOperation2();
$obj->calculations($input1,$input2);

(4)Try & Catch : The try block consists the block of code due to exception generated and catch is used for handling or performing action in the respect of that particular exception

Example:
try{
$num=2/0;
}catch(Exception $e){
echo $e->getMessage();
}

(5)Interface: Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as “polymorphism”.

Example:
interface student{
public function studentDetails($student_name,$course);
}
class marks implements student{
public function studentDetails($student_name,$course)
{
echo “Student Name: “.$student_name;
echo “Course: “.$course;
}
}
$stuObj= new marks();
$stuObj->studentDetails(“Siddhartha”,”MCA”);

(6)Constructor: A constructor allows you to initialize an object’s properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Example:
class student{
public function __construct(){
$this->student_name=”Siddhartha”;
$this->course=”MCA”;
}
public function studentDetails()
{
echo “Student Name: “.$this->student_name;
echo “Course: “.$this->course;
}
}
$stuObj= new student();
$stuObj->studentDetails();

(7)Destructor: A destructor is called when the object is destructed or the script is stopped or exited. If you create a __destruct() function, PHP will automatically call this function at the end of the script.
class student{
public function __construct(){
$this->student_name=”Siddhartha”;
$this->course=”MCA”;
}
public function __destruct(){
echo “Class Object Has Been Destroyed…”;
}
public function studentDetails()
{
echo “Student Name: “.$this->student_name;
echo “Course: “.$this->course;
}
}
$stuObj= new student();
$stuObj->studentDetails();

(8)trait: PHP only supports single inheritance: a child class can inherit only from one single parent. So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem. Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
Example:
trait marks{
public function marksDetails(){
echo “Maths: 98”;
echo “Science: 88”;
}
}
class student{
use marks;
public function studentDetails()
{
echo “Student Name: Siddhartha”;
echo “Course: M.C.A”;
}
}
$stuObj= new student();
$stuObj->studentDetails();
$stuObj->marksDetails();

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *