Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Interface In PHP
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Object Oriented Tutorials
  3. Interface In PHP

PHP Object Oriented Tutorial

Basic Object Oriented Tutorials

  • Object Oriented Programming In PHP
  • Advantage of OOPS In PHP
  • Important OOPS concepts
  • Oops Features In PHP
  • Class In PHP
  • Object In PHP
  • Constructor and Destructor
  • Inheritance in PHP
  • Encapsulation In Php
  • Abstract Classes In PHP
  • Interface In PHP
  • Polymorphism In Php

Interface In PHP

The interface declares what methods a class must have without having to implement them. Any class that implements the interface will have to implement details of those declared methods. Interface is not a class, so you can not instantiate an interface. It is useful when you need to enforce some classes to do something.

  • An interface is a description of the actions that an object can do.
  • Interface is written in the same way as the class the declaration with interface keyword.

Rules of Interfaces:

  • All methods declared in an interface must be public; this is the nature of an interface.
  • All methods in the interface must be implemented within a class; failure to do so will result in a fatal error.
  • The class implementing the interface must use the exact same method signatures as are defined in the interface
  • Interfaces can be extended like classes using the extends operator.

 

Example for the interface class

<?php
	interface A {
		public function setProperty($x);
		public function description();
	}
	class Property implements A {
		public function setProperty($x) {
			$this->x = $x;
		}
		public function description() {
			echo "Counting" . $this->x . plot;
		}
	}
	$property = new Property();
	$property->setProperty(Garden);
	$property->description();
?>


Output:

Counting Garden plot


Interface can be extended with another interface using extends keyword

<?php
	interface A {
		public function Compute();
	}
	interface B extends A {
		public function Divide();
	}
	class C implements B {
		public function Divide() {
			$var=10;
			$var1=2;
			$var3=$var/$var1;
			echo “division of 10/2 is” . $var3;
		}
		public function Compute() {
			$a=2;
			$b=3;
			$c=$a*$b;
			echo “multiplication of 2*3 is” . $c;
		}
	}
	$obj = new C();
	$obj->Divide();
	$obj->Compute();
?>


Output:

division of 10/2 is 5
multiplication of 2*3 is 6

Interfaces Important Point:-

  • We cannot create objects to interface, but the class implementing the interface can have objects
  • We cannot define a variable in an interface.
  • If we extend interface all the methods of the interface must be implemented in the child class.
Series: PHP Object Oriented Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
Interface in php why we use interface in php interface in php easytolearning difference between abstract class and interface in php with example php interface tutorial how to include interface in php php interface properties interface in oops

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.