PHP OOP
Classes
It’s recommended that you make the class name the same as the file name, and writing it in StudlyCaps according to PSR-1 Standard.
1<?php
2
3class TestClass{
4
5}
Functions/Methods
By default functions are public dynamic, in this case you will need to make an object out of TestClass with the keyword new, to use it’s dynamic functions.
1<?php
2
3class TestClass{
4
5 function SayHey(){
6 echo "Hello\n";
7 }
8}
9
10$test = new TestClass;
11$test->SayHey();
Static Functions/Methods
This is an example of a static function, As you can see you don’t have to make an Object out of the TestClass Class to ba able to call it.
1<?php
2
3class TestClass{
4
5 static public function SayHey(){
6 echo "Hello\n";
7 }
8}
9
10TestClass::SayHey();
Constructs and Destructs
These are special types of methods you can use in your PHP Classes.
- Constructs get called when you initialize a class with the new keyword.
- Destructs get called before the object get destroyed.
1<?php
2
3class TestClass{
4 function __construct(){
5 echo "object initialized\n";
6 }
7 function __destruct(){
8 echo "object destroyed\n";
9 }
10 function SayHey(){
11 echo "Hello\n";
12 }
13}
14
15$test = new TestClass();