PHP private Keyword
Example
Use private
to prevent outside code or derived classes from modifying a
property:
<?php
class MyClass {
private $number = 0;
public function
add1() {
$this->number++;
}
public function getNumber() {
return $this->number;
}
}
$obj = new MyClass();
$obj->add1();
echo "The number is "
. $obj->getNumber();
?>
Try it Yourself »
Definition and Usage
The private
keyword is an access modifier. It marks a property or method as private.
Private properties and methods can only be used by the class in which the property or method was defined. Derived classes and outside code cannot use them.
Related Pages
The protected
keyword
The public
keyword
Learn more about access modifiers in our PHP OOP - Access Modifiers Tutorial.
❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.