PHP public Keyword

❮ PHP Keywords

Example

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 »

Definition and Usage

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.


Related Pages

The protected keyword

The private keyword

Learn more about access modifiers in our PHP OOP - Access Modifiers Tutorial.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.