Cheatsheet Oop PHP
Cheatsheet Oop PHP
1 of 2 5/11/2022, 1:09 PM
Firefox https://www.codecademy.com/learn/learn-php/modules/classes-and-objec...
PHP class
In PHP, classes are de�ned using the class keyword.
Functions de�ned within a class become methods and // Test class
variables within the class are considered properties. class Test {
There are three levels of visibility for class members: public $color = "blue";
protected $shape = "sphere";
public (default) - accessible from outside of the
private $quantity = 10;
class
public static $number = 42;
protected - only accessible within the class or its public static function returnHello() {
descendants
return "Hello";
private - only accessible within the de�ning class }
}
Members can be de�ned to be static:
Classes are instantiated into objects using the new // only color can be accessed from the
keyword. Members of an object are accessed using the instance
Object Operator ( -> ). echo $object->color; # Works
echo $object->shape; # Fails
echo $object->quantity; # Fails
echo $object->number; # Fails
2 of 2 5/11/2022, 1:09 PM