GonMAX

PHP CheatSheet

Nov 25th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.24 KB | None | 0 0
  1. <?php
  2. // Exit the file, string inside get's echo'ed
  3. die("This file is not ment to be ran. ¯\_(ツ)_/¯");
  4. exit("This file is not ment to be ran. ¯\_(ツ)_/¯");
  5. /**
  6.  * Printing
  7.  */
  8. echo ""; // Print a string or type that can be made into a string(I.E int, float).
  9. print_r($arr); // Print anything, with type hints for array's and object's
  10. var_dump($arr); // Print anything, with type hints for any value and sizes
  11. /**
  12.  * Ways of looping
  13.  */
  14. continue; // Skip current iter
  15. break; // Exit loop
  16. // Foreach
  17. foreach($arr as $key => $value) {
  18.     $key = $key;
  19.     $value = $value;
  20. }
  21. // For
  22. for($i = 0; $i < count($arr) - 1; $i++) {
  23.     $key = $i;
  24.     $value = $arr[$i];
  25. }
  26. // While
  27. $i = 0;
  28. while($i < count($arr) - 1) {
  29.     $key = $i;
  30.     $value = $arr[$i];
  31. }
  32. // Do while
  33. $i = 0;
  34. do {
  35.     $key = $i;
  36.     $value = $arr[$i];
  37. } while($i < count($arr));
  38. // Switch
  39. switch($arr) {
  40.     case 1:
  41.         break;
  42.     case 2:
  43.         break;
  44.     case 3:
  45.         break;
  46.     default:
  47. }
  48. /**
  49.  * Global variables
  50.  * http://php.net/manual/en/language.variables.superglobals.php
  51.  */
  52. $_SERVER; // SERVER variables
  53. $_GET; // Query params
  54. $_POST; // Post fields
  55. $_REQUEST; // GET and POST together
  56. $GLOBALS; // Array of global variables
  57. $_SESSION; // Browser session
  58. $_FILES; // Array of files that are sent in request
  59. $_COOKIE; // Array of cookies sent in request
  60. $_ENV; // php.ini options
  61. $argv; // Array of terminal arguments (filename included)
  62. $argc; // Number of arguments passed into terminal
  63. /**
  64.  * Class
  65.  * http://php.net/manual/en/language.oop5.basic.php
  66.  */
  67. class NormalClass extends AbstractClassName implements InterfaceName
  68. {
  69.     use TraitName;
  70.     // --> PROPERTY TYPES <--
  71.     /**
  72.      * Public property, everyone can access this property.
  73.      * @var Type
  74.      */
  75.     public $property;
  76.     /**
  77.      * Private property, only this instance can access this property.
  78.      * @var Type
  79.      */
  80.     private $property;
  81.     /**
  82.      * Protected property, this instance and childs can access this property.
  83.      * @var Type
  84.      */
  85.     protected $property;
  86.     /**
  87.      * Static property, is the same for all instances of this class.
  88.      * @var Type
  89.      */
  90.     static $property;
  91.     // --> FUNCTION TYPES <--
  92.     /**
  93.      * Public function, everyone can access this function.
  94.      * @param Type
  95.      * @return Type
  96.      */
  97.     public function publicFunction(Type $var = null): Type
  98.     {
  99.     }
  100.     /**
  101.      * Private function, only this instance can access this function.
  102.      * @param Type
  103.      * @return Type
  104.      */
  105.     private function privateFunction(Type $var = null): Type
  106.     {
  107.     }
  108.     /**
  109.      * Protected function, this instance and childs can access this function.
  110.      * @param Type
  111.      * @return Type
  112.      */
  113.     protected function protectedFunction(Type $var = null): Type
  114.     {
  115.     }
  116.    
  117.     /**
  118.      * Static function, doesn't need an instance to be executed.
  119.      * @param Type
  120.      * @return Type
  121.      */
  122.     public static function staticFunction(Type $var = null): Type
  123.     {
  124.     }
  125.     // --> MAGIC METHODS <--
  126.     /**
  127.      * Gets triggered on creating a new class instance
  128.      * http://php.net/manual/en/language.oop5.decon.php
  129.      * @param Type
  130.      * @return void
  131.      */
  132.     public function __construct(Type $var = null)
  133.     {
  134.     }
  135.     /**
  136.      * Gets triggered on destruction of a class instance
  137.      * http://php.net/manual/en/language.oop5.decon.php
  138.      * @return void
  139.      */
  140.     public function __destruct()
  141.     {
  142.     }
  143.     /**
  144.      * __set() is run when writing data to inaccessible properties.
  145.      * http://php.net/manual/en/language.oop5.overloading.php
  146.      * @param string name
  147.      * @param mixed value
  148.      * @return void
  149.      */
  150.     public function __set(string $name , mixed $value)
  151.     {
  152.     }
  153.     /**
  154.      * __get() is utilized for reading data from inaccessible properties.
  155.      * http://php.net/manual/en/language.oop5.overloading.php
  156.      * @param string name
  157.      * @return mixed
  158.      */
  159.     public function __get(string $name)
  160.     {
  161.     }
  162.     /**
  163.      * __isset() is triggered by calling isset() or empty() on inaccessible properties.
  164.      * http://php.net/manual/en/language.oop5.overloading.php
  165.      * @param string name
  166.      * @return bool
  167.      */
  168.     public function __isset(string $name)
  169.     {
  170.     }
  171.     /**
  172.      * __unset() is invoked when unset() is used on inaccessible properties.
  173.      * http://php.net/manual/en/language.oop5.overloading.php
  174.      * @param string name
  175.      * @return void
  176.      */
  177.     public function __unset(string $name)
  178.     {
  179.     }
  180.     /**
  181.      * __call is triggered when invoking inaccessible methods in an object context.
  182.      * http://php.net/manual/en/language.oop5.overloading.php
  183.      * @param string name
  184.      * @param array arguments
  185.      * @return mixed
  186.      */
  187.     public function __call(string $name, array $arguments)
  188.     {
  189.     }
  190.     /**
  191.      * __callStatic() is triggered when invoking inaccessible methods in a static context.
  192.      * http://php.net/manual/en/language.oop5.overloading.php
  193.      * @param string name
  194.      * @param array arguments
  195.      * @return mixed
  196.      */
  197.     public static function __callStatic(string $name, array $arguments)
  198.     {
  199.     }
  200.     /**
  201.      * http://php.net/manual/en/language.oop5.magic.php
  202.      * @return array
  203.      */
  204.     public function __sleep()
  205.     {
  206.     }
  207.     /**
  208.      * http://php.net/manual/en/language.oop5.magic.php
  209.      * @return void
  210.      */
  211.     public function __wakeup()
  212.     {
  213.     }
  214.     /**
  215.      * http://php.net/manual/en/language.oop5.magic.php
  216.      * @return string
  217.      */
  218.     public function __toString()
  219.     {
  220.     }
  221.     /**
  222.      * http://php.net/manual/en/language.oop5.magic.php
  223.      * @param Type
  224.      * @return mixed
  225.      */
  226.     public function __invoke(Type $var = null)
  227.     {
  228.     }
  229.     /**
  230.      * http://php.net/manual/en/language.oop5.magic.php
  231.      * @param array properties
  232.      * @return object
  233.      */
  234.     public static function __set_state(array $properties)
  235.     {
  236.     }
  237.     /**
  238.      * http://php.net/manual/en/language.oop5.magic.php
  239.      * @return array
  240.      */
  241.     public function __debugInfo()
  242.     {
  243.     }
  244. }
  245. /**
  246.  * Every class that has implemented this interface need to have the same functions.
  247.  */
  248. interface InterfaceName
  249. {
  250.     public function FunctionName(Type $var = null): Type;
  251. }
  252. /**
  253.  * Combination of class and interface.
  254.  */
  255. abstract class AbstractClassName
  256. {
  257.     /**
  258.      * Classes extending this abstract class need to have this function.
  259.      * @param Type
  260.      * @return Type
  261.      */
  262.     abstract function abstractFunction(Type $var = null): Type;
  263. }
  264. /**
  265.  * Basic Implementation of LoggerAwareInterface.
  266.  * @see https://github.com/php-fig/log/blob/master/Psr/Log/LoggerAwareTrait.php
  267.  */
  268. trait LoggerAwareTrait
  269. {
  270.     /**
  271.      * The logger instance.
  272.      *
  273.      * @var LoggerInterface
  274.      */
  275.     protected $logger;
  276.     /**
  277.      * Sets a logger.
  278.      *
  279.      * @param LoggerInterface $logger
  280.      */
  281.     public function setLogger(LoggerInterface $logger)
  282.     {
  283.         $this->logger = $logger;
  284.     }
  285. }
  286. /**
  287.  * Example with use of LoggerAwareTrait.
  288.  */
  289. class ClassWithLogger
  290. {
  291.     /**
  292.      * Use the LoggerAwareTrait in this class.
  293.      */
  294.     use LoggerAwareTrait;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment