Use public
to declare a property that can be modified by any code:
<?php
class MyClass {
public $number = 0;
}
$obj = new MyClass();
$obj->number = 5;
echo "The number is " . $obj->number;
?>
Try it Yourself »
The public
keyword is an access modifier. It marks a property or method as public.
Public properties and methods can be used by any code that can access the object.
The protected
keyword
The private
keyword
Learn more about access modifiers in our PHP OOP - Access Modifiers Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!