Access modifiers in PHP are keywords used to specify the visibility of a property or method. They define how and from where these properties and methods can be accessed.
Properties and methods declared as public can be accessed from anywhere, both inside and outside of the class.
class MyClass {
public $name;
public function showName() {
return $this->name;
}
}
$obj = new MyClass();
$obj->name = "John";
echo $obj->showName(); // Output: John
Private properties and methods can only be accessed within the class that defines them. They are not accessible from outside the class.
class MyClass {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$obj = new MyClass();
$obj->setName("Jane");
echo $obj->getName(); // Output: Jane
Protected properties and methods can be accessed within the class itself and by inheriting classes. They are not accessible from outside.
class ParentClass {
protected $name;
protected function setName($name) {
$this->name = $name;
}
}
class ChildClass extends ParentClass {
public function setNameAndShow($name) {
$this->setName($name);
return $this->name;
}
}
$obj = new ChildClass();
echo $obj->setNameAndShow("Alice"); // Output: Alice
In PHP, if no access modifier is specified, the property or method is treated as public by default.
class MyClass {
var $name; // Default is public
function showName() {
return $this->name;
}
}
$obj = new MyClass();
$obj->name = "Bob";
echo $obj->showName(); // Output: Bob
When a class inherits from another, it can access the protected and public members of the parent class. Private members remain inaccessible.
class BaseClass {
protected $name;
public function setName($name) {
$this->name = $name;
}
}
class DerivedClass extends BaseClass {
public function getName() {
return $this->name;
}
}
$obj = new DerivedClass();
$obj->setName("Charlie");
echo $obj->getName(); // Output: Charlie
In PHP, all methods declared in an interface must be public. Interfaces do not allow private or protected methods.
interface MyInterface {
public function myMethod();
}
class MyClass implements MyInterface {
public function myMethod() {
return "Implemented Method";
}
}
$obj = new MyClass();
echo $obj->myMethod(); // Output: Implemented Method
Static methods can have any access modifier. They belong to the class rather than any object instance.
class MyClass {
public static function myStaticMethod() {
return "Static Method";
}
}
echo MyClass::myStaticMethod(); // Output: Static Method
A method declared as final cannot be overridden by child classes. It can have any access modifier.
class MyClass {
final public function myFinalMethod() {
return "Final Method";
}
}
class ChildClass extends MyClass {
// Cannot override myFinalMethod
}
$obj = new MyClass();
echo $obj->myFinalMethod(); // Output: Final Method
Abstract methods must be declared with a visibility keyword. They are intended to be implemented in derived classes.
abstract class MyClass {
abstract protected function myAbstractMethod();
}
class ConcreteClass extends MyClass {
protected function myAbstractMethod() {
return "Implemented Abstract Method";
}
}
$obj = new ConcreteClass();
echo $obj->myAbstractMethod(); // Output: Implemented Abstract Method
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