In PHP, a class is a blueprint for creating objects. It defines properties and methods that the created objects can use.
A class is defined using the <class>
keyword followed by the class name and a pair of curly braces.
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
An object is created using the <new>
keyword followed by the class name.
$myCar = new Car("black", "Volvo");
echo $myCar->message();
Console Output:
My car is a black Volvo!
Properties are variables that belong to a class. They are defined within the class and can have different visibility levels.
PHP supports three types of access modifiers: public
, protected
, and private
.
class Fruit {
public $name;
protected $color;
private $weight;
}
Public properties can be accessed from anywhere, both inside and outside the class.
$apple = new Fruit();
$apple->name = "Apple"; // OK
Console Output:
Apple
Methods are functions that are defined inside a class. They define the behavior of the class objects.
Define a method within a class to perform specific operations or actions.
class Dog {
public $name;
public function bark() {
return "Woof! Woof!";
}
}
Methods are called using the ->
operator on the object.
$dog = new Dog();
echo $dog->bark(); // Outputs: Woof! Woof!
Console Output:
Woof! Woof!
Constructors are special methods that are automatically called when an object is created. They initialize object properties.
Constructors in PHP are defined using the __construct()
method.
class Cat {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
When creating an object, parameters can be passed to the constructor to set initial values.
$cat = new Cat("Whiskers");
echo $cat->name; // Outputs: Whiskers
Console Output:
Whiskers
Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse.
A class that is inherited is called a base class, and the class that inherits is called a derived class.
class Animal {
public function makeSound() {
return "Some sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
Derived classes can override methods from the base class to provide specific implementations.
$dog = new Dog();
echo $dog->makeSound(); // Outputs: Bark
Console Output:
Bark
Static properties and methods belong to the class rather than any object instance. They are accessed using the scope resolution operator ::
.
Static members are declared using the static
keyword.
class MathOperations {
public static $pi = 3.14;
public static function square($x) {
return $x * $x;
}
}
Static members can be accessed without creating an object of the class.
echo MathOperations::$pi; // Outputs: 3.14
echo MathOperations::square(5); // Outputs: 25
Console Output:
3.14
25
Abstract classes cannot be instantiated directly. They are designed to be extended by other classes.
Abstract methods are declared in an abstract class and must be defined in derived classes.
abstract class Vehicle {
abstract public function startEngine();
}
class Car extends Vehicle {
public function startEngine() {
return "Engine started";
}
}
Derived classes must implement all abstract methods from their parent abstract class.
$car = new Car();
echo $car->startEngine(); // Outputs: Engine started
Console Output:
Engine started
Interfaces allow you to specify what methods a class should implement. They provide a way to enforce certain behaviors across classes.
Interfaces are defined using the interface
keyword.
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
return "Meow";
}
}
Classes implementing an interface must define all its methods.
$cat = new Cat();
echo $cat->makeSound(); // Outputs: Meow
Console Output:
Meow
Traits are a mechanism for code reuse in single inheritance languages like PHP. They allow you to include methods in multiple classes.
Traits are defined using the trait
keyword.
trait Logger {
public function log($message) {
echo $message;
}
}
class Application {
use Logger;
}
Traits are included in classes using the use
keyword.
$app = new Application();
$app->log("Application started"); // Outputs: Application started
Console Output:
Application started
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies