From 64aa38a1d28d6e3b9f770e97476f0111f180bd3e Mon Sep 17 00:00:00 2001 From: Akshay Kalose Date: Fri, 16 Oct 2015 22:07:08 -0400 Subject: Add More Magic Methods in PHP --- php.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 39ec5aef..78d9d1f2 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -498,10 +498,23 @@ class MyClass print 'MyClass'; } - //final keyword would make a function unoverridable + // final keyword would make a function unoverridable final function youCannotOverrideMe() { } + + // Magic Methods + + // what to do if Object is treated as a String + public function __toString() { + return $property; + } + + // opposite to __construct() + // called when object no longer referenced + public function __destruct() { + print "Destroying" + } /* * Declaring class properties or methods as static makes them accessible without -- cgit v1.2.3 From d9f3b13e278eebb173253c756ffe847b3188f4af Mon Sep 17 00:00:00 2001 From: Akshay Kalose Date: Sun, 18 Oct 2015 17:37:39 -0400 Subject: Fix Grammar and Syntax --- php.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 78d9d1f2..ac0a83b9 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -511,9 +511,9 @@ class MyClass } // opposite to __construct() - // called when object no longer referenced + // called when object is no longer referenced public function __destruct() { - print "Destroying" + print "Destroying"; } /* -- cgit v1.2.3 From ed4ac31d97b36866a48faf3b9328c320ba55074f Mon Sep 17 00:00:00 2001 From: Sawyer Charles Date: Sun, 18 Oct 2015 17:46:23 -0500 Subject: Added variable amount of parameters as of php 5.6+ --- php.html.markdown | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 5bc2ddce..e7b57b4b 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -445,6 +445,16 @@ function parameters() { parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | +// Since PHP 5.6 you can get a variable number of arguments +function variable($word, ...$list) { + echo $word . " || "; + foreach ($list as $item) { + echo $item . ' | '; + } +} + +variable("Separate", "Hello", "World") // Separate || Hello | world | + /******************************** * Includes */ -- cgit v1.2.3 From b4c8ff365ee1d633470ced72de5f540744fa921a Mon Sep 17 00:00:00 2001 From: Sawyer Charles Date: Sun, 18 Oct 2015 17:47:42 -0500 Subject: capital letter --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index e7b57b4b..71f23871 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -453,7 +453,7 @@ function variable($word, ...$list) { } } -variable("Separate", "Hello", "World") // Separate || Hello | world | +variable("Separate", "Hello", "World") // Separate || Hello | World | /******************************** * Includes -- cgit v1.2.3 From 087dbecb2f25c2d372ed4e007f7641cbc87c0571 Mon Sep 17 00:00:00 2001 From: Tom Duff Date: Sun, 18 Oct 2015 20:21:42 -0400 Subject: Add more examples and explanations Added some clarifying examples to sections on echo(), constants, and cleaned up formatting on others. Added further explanation about the spaceship operator. --- php.html.markdown | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 5bc2ddce..127e601b 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -104,7 +104,8 @@ END; echo 'This string ' . 'is concatenated'; // Strings can be passed in as parameters to echo -echo 'Multiple', 'Parameters', 'Valid'; +echo 'Multiple', 'Parameters', 'Valid'; // Returns 'MultipleParametersValid' + /******************************** * Constants @@ -117,8 +118,10 @@ echo 'Multiple', 'Parameters', 'Valid'; // followed by any number of letters, numbers, or underscores. define("FOO", "something"); -// access to a constant is possible by direct using the choosen name -echo 'This outputs '.FOO; +// access to a constant is possible by calling the choosen name without a $ +echo FOO; // Returns 'something' +echo 'This outputs '.FOO; // Returns 'This ouputs something' + /******************************** @@ -159,9 +162,9 @@ echo('Hello World!'); print('Hello World!'); // The same as echo -// echo is actually a language construct, so you can drop the parentheses. +// echo and print are language constructs too, so you can drop the parentheses echo 'Hello World!'; -print 'Hello World!'; // So is print +print 'Hello World!'; $paragraph = 'paragraph'; @@ -219,7 +222,11 @@ assert($a !== $d); assert(1 === '1'); assert(1 !== '1'); -// spaceship operator since PHP 7 +// 'Spaceship' operator (since PHP 7) +// Returns 0 if values on either side are equal +// Returns 1 if value on the left is greater +// Returns -1 if the value on the right is greater + $a = 100; $b = 1000; -- cgit v1.2.3 From c69ef3115bb900a5b2f716ebd9791a38ca113ab7 Mon Sep 17 00:00:00 2001 From: labrack Date: Thu, 22 Oct 2015 13:06:15 -0400 Subject: [php/en] Un-beef a comment block closing Somebody beefed a closing on the 'Late Static Binding' Comment block, causing the next 18 or so lines to be included in the comment instead of parsed for syntax properly. --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 127e601b..403fc41d 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -723,7 +723,7 @@ $cls = new SomeOtherNamespace\MyClass(); /********************** * Late Static Binding * -* / +*/ class ParentClass { public static function who() { -- cgit v1.2.3 From 5afca01bdfb7dab2e6086a63bb80444aae9831cb Mon Sep 17 00:00:00 2001 From: Liam Demafelix Date: Fri, 30 Oct 2015 23:26:49 +0800 Subject: [php/en] Line 159 - echo isn't a function, print() is Line 337 - unneeded semicolon (it's a pre-test loop) --- php.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 0504ced2..7b0cf61c 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -3,6 +3,7 @@ language: PHP contributors: - ["Malcolm Fell", "http://emarref.net/"] - ["Trismegiste", "https://github.com/Trismegiste"] + - [ Liam Demafelix , https://liamdemafelix.com/] filename: learnphp.php --- @@ -156,14 +157,13 @@ unset($array[3]); * Output */ -echo('Hello World!'); +echo 'Hello World!'; // Prints Hello World! to stdout. // Stdout is the web page if running in a browser. print('Hello World!'); // The same as echo -// echo and print are language constructs too, so you can drop the parentheses -echo 'Hello World!'; +// print is a language construct too, so you can drop the parentheses print 'Hello World!'; $paragraph = 'paragraph'; @@ -335,7 +335,7 @@ switch ($x) { $i = 0; while ($i < 5) { echo $i++; -}; // Prints "01234" +} // Prints "01234" echo "\n"; -- cgit v1.2.3 From c23fa3613874bc22b65e5506c374c8f8bf2691d8 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 31 Oct 2015 19:34:51 +0800 Subject: Revert "[php/en]" This reverts commit 5afca01bdfb7dab2e6086a63bb80444aae9831cb. --- php.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 7b0cf61c..0504ced2 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -3,7 +3,6 @@ language: PHP contributors: - ["Malcolm Fell", "http://emarref.net/"] - ["Trismegiste", "https://github.com/Trismegiste"] - - [ Liam Demafelix , https://liamdemafelix.com/] filename: learnphp.php --- @@ -157,13 +156,14 @@ unset($array[3]); * Output */ -echo 'Hello World!'; +echo('Hello World!'); // Prints Hello World! to stdout. // Stdout is the web page if running in a browser. print('Hello World!'); // The same as echo -// print is a language construct too, so you can drop the parentheses +// echo and print are language constructs too, so you can drop the parentheses +echo 'Hello World!'; print 'Hello World!'; $paragraph = 'paragraph'; @@ -335,7 +335,7 @@ switch ($x) { $i = 0; while ($i < 5) { echo $i++; -} // Prints "01234" +}; // Prints "01234" echo "\n"; -- cgit v1.2.3 From b307b65e8df4e27991c8cf7ade1e3d7faa1e87fd Mon Sep 17 00:00:00 2001 From: Anton Ivanov Date: Sat, 31 Oct 2015 14:37:12 +0300 Subject: Add binary number example. --- php.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 7b0cf61c..d03b89fe 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -54,6 +54,8 @@ $int1 = 12; // => 12 $int2 = -12; // => -12 $int3 = 012; // => 10 (a leading 0 denotes an octal number) $int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal) +// Binary integer literals are available since PHP 5.4.0. +$int5 = 0b11111111; // 255 (a leading 0b denotes a binary number) // Floats (aka doubles) $float = 1.234; @@ -117,11 +119,11 @@ echo 'Multiple', 'Parameters', 'Valid'; // Returns 'MultipleParametersValid' // a valid constant name starts with a letter or underscore, // followed by any number of letters, numbers, or underscores. -define("FOO", "something"); +define("FOO", "something"); // access to a constant is possible by calling the choosen name without a $ echo FOO; // Returns 'something' -echo 'This outputs '.FOO; // Returns 'This ouputs something' +echo 'This outputs ' . FOO; // Returns 'This ouputs something' -- cgit v1.2.3 From db690c17c5335b746e3e7867e3d7cf5aab724a9a Mon Sep 17 00:00:00 2001 From: Rudy Affandi Date: Sat, 31 Oct 2015 10:54:55 -0700 Subject: Add PHP magic constants entry --- php.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 0504ced2..06a289f4 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -765,6 +765,13 @@ I'm a ParentClass But I'm ChildClass */ +/********************** +* Magic constants +* +*/ + +// Get directory of the file +require __DIR__ . '/vendor/autoload.php'; /********************** * Error Handling -- cgit v1.2.3 From dc1c759c2903f15fd3c52c2241c765c65fbe9d89 Mon Sep 17 00:00:00 2001 From: Rudy Affandi Date: Sat, 31 Oct 2015 11:27:13 -0700 Subject: Add the rest of the magic constants --- php.html.markdown | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 06a289f4..5034cfd1 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -770,8 +770,32 @@ But I'm ChildClass * */ -// Get directory of the file -require __DIR__ . '/vendor/autoload.php'; +// Get current class name. Must be used inside a class declaration. +echo "Current class name is " . __CLASS__; + +// Get full path directory of a file +echo "Current directory is " . __DIR__; + + // Typical usage + require __DIR__ . '/vendor/autoload.php'; + +// Get full path of a file +echo "Current file path is " . __FILE__; + +// Get current function name +echo "Current function name is " . __FUNCTION__; + +// Get current line number +echo "Current line number is " . __LINE__; + +// Get the name of the current method. Only returns a value when used inside a trait or object declaration. +echo "Current method is " . __METHOD__; + +// Get the name of the current namespace +echo "Current namespace is " . __NAMESPACE__; + +// Get the name of the current trait. Only returns a value when used inside a trait or object declaration. +echo "Current namespace is " . __TRAIT__; /********************** * Error Handling -- cgit v1.2.3 From 663f6e28f54a106204b9656d7a1ece1e9f225bcf Mon Sep 17 00:00:00 2001 From: Patrick Lee Date: Thu, 18 Feb 2016 15:59:00 -0500 Subject: Update php.html.markdown Added reference to add an element to an associative array. --- php.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index ce178a15..0be8b427 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -146,6 +146,9 @@ echo $associative['One']; // prints 1 $array = ['One', 'Two', 'Three']; echo $array[0]; // => "One" +// Add an element to an associative array +$array['Four'] = 4; + // Add an element to the end of an array $array[] = 'Four'; // or -- cgit v1.2.3 From a5ae0795ba9e947b6b766707a0b93e5032ba8615 Mon Sep 17 00:00:00 2001 From: Patrick Lee Date: Thu, 18 Feb 2016 16:30:21 -0500 Subject: Update php.html.markdown Moved to group with associative arrays. --- php.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 0be8b427..4acdef98 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -142,13 +142,13 @@ $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; echo $associative['One']; // prints 1 +// Add an element to an associative array +$array['Four'] = 4; + // List literals implicitly assign integer keys $array = ['One', 'Two', 'Three']; echo $array[0]; // => "One" -// Add an element to an associative array -$array['Four'] = 4; - // Add an element to the end of an array $array[] = 'Four'; // or -- cgit v1.2.3 From c50ff1ddc874ace092507982f881510b27a2e173 Mon Sep 17 00:00:00 2001 From: Patrick Lee Date: Thu, 18 Feb 2016 16:38:15 -0500 Subject: Update php.html.markdown Changed var name to $associative --- php.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'php.html.markdown') diff --git a/php.html.markdown b/php.html.markdown index 4acdef98..6944390c 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -143,7 +143,7 @@ $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; echo $associative['One']; // prints 1 // Add an element to an associative array -$array['Four'] = 4; +$associative['Four'] = 4; // List literals implicitly assign integer keys $array = ['One', 'Two', 'Three']; -- cgit v1.2.3