2013-06-27 13:42:07 +04:00
|
|
|
---
|
|
|
|
language: php
|
|
|
|
author: Malcolm Fell
|
|
|
|
author_url: http://emarref.net/
|
2013-06-30 07:19:14 +04:00
|
|
|
filename: learnphp.php
|
2013-06-27 13:42:07 +04:00
|
|
|
---
|
|
|
|
|
|
|
|
This document describes PHP 5+.
|
|
|
|
|
|
|
|
```php
|
2013-06-30 01:34:54 +04:00
|
|
|
<?php // PHP code must be enclosed with <?php ? > tags
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
// Two forward slashes start a one-line comment.
|
|
|
|
|
|
|
|
# So will a hash (aka pound symbol) but // is more common
|
|
|
|
|
|
|
|
/*
|
|
|
|
Surrounding text in slash-asterisk and asterisk-slash
|
|
|
|
makes it a multi-line comment.
|
|
|
|
*/
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Use "echo" or "print" to print output
|
|
|
|
print('Hello '); // Prints "Hello " with no line break
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// () are optional for print and echo
|
|
|
|
echo 'World\n'; // Prints "World" with a line break
|
|
|
|
// (all statements must end with a semicolon)
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Anything outside <?php tags is echoed automatically
|
|
|
|
?>Hello World Again!
|
2013-06-27 20:49:03 +04:00
|
|
|
<?php
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
|
|
|
|
/************************************
|
|
|
|
* Types & Variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Variables begin with the $ symbol.
|
|
|
|
// A valid variable name starts with a letter or underscore,
|
|
|
|
// followed by any number of letters, numbers, or underscores.
|
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
// Boolean values are case-insensitive
|
|
|
|
$boolean = true; // or TRUE or True
|
|
|
|
$boolean = false; // or FALSE or False
|
|
|
|
|
|
|
|
// Integers
|
2013-06-30 01:34:54 +04:00
|
|
|
$int1 = 19; // => 19
|
|
|
|
$int2 = -19; // => -19
|
|
|
|
$int3 = 019; // => 15 (a leading 0 denotes an octal number)
|
|
|
|
$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
// Floats (aka doubles)
|
2013-06-28 00:48:31 +04:00
|
|
|
$float = 1.234;
|
|
|
|
$float = 1.2e3;
|
2013-06-27 13:42:07 +04:00
|
|
|
$float = 7E-10;
|
|
|
|
|
|
|
|
// Arithmetic
|
2013-06-30 01:34:54 +04:00
|
|
|
$sum = 1 + 1; // 2
|
|
|
|
$difference = 2 - 1; // 1
|
|
|
|
$product = 2 * 2; // 4
|
|
|
|
$quotient = 2 / 1; // 2
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
// Shorthand arithmetic
|
2013-06-30 01:34:54 +04:00
|
|
|
$number = 0;
|
2013-06-27 20:49:03 +04:00
|
|
|
$number += 1; // Add 1 to $number
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $number++; // Prints 1 (increments after evaluation)
|
|
|
|
echo ++$number; // Prints 3 (increments before evalutation)
|
|
|
|
$number /= $float; // Divide and assign the quotient to $number
|
2013-06-27 20:49:03 +04:00
|
|
|
|
|
|
|
// Strings should be enclosed in single quotes;
|
|
|
|
$sgl_quotes = '$String'; // => '$String'
|
|
|
|
|
|
|
|
// Avoid using double quotes except to embed other variables
|
2013-06-30 01:34:54 +04:00
|
|
|
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String'
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Special characters are only escaped in double quotes
|
2013-06-27 20:49:03 +04:00
|
|
|
$escaped = "This contains a \t tab character.";
|
2013-06-30 01:34:54 +04:00
|
|
|
$unescaped = 'This just contains a slash and a t: \t';
|
2013-06-27 20:49:03 +04:00
|
|
|
|
|
|
|
// Enclose a variable in curly braces if needed
|
2013-06-30 01:34:54 +04:00
|
|
|
$money = "I have $${number} in the bank.";
|
2013-06-27 20:49:03 +04:00
|
|
|
|
|
|
|
// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
|
2013-06-27 13:42:07 +04:00
|
|
|
$nowdoc = <<<'END'
|
|
|
|
Multi line
|
|
|
|
string
|
|
|
|
END;
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Heredocs will do string interpolation
|
2013-06-27 13:42:07 +04:00
|
|
|
$heredoc = <<<END
|
|
|
|
Multi line
|
|
|
|
$sgl_quotes
|
2013-06-30 01:34:54 +04:00
|
|
|
END;
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// String concatenation is done with .
|
|
|
|
echo 'This string ' . 'is concatenated';
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Arrays
|
|
|
|
*/
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// All arrays in PHP are associative arrays (hashmaps),
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
// Associative arrays, known as hashmaps in some languages.
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Works with all PHP versions
|
|
|
|
$associative = array('One' => 1, 'Two' => 2, 'Three' => 3);
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// PHP 5.4 introduced a new syntax
|
|
|
|
$associative = ['One' => 1, 'Two' => 2, 'Three' => 3];
|
|
|
|
|
|
|
|
echo $associative['One']; // prints "1"
|
|
|
|
|
|
|
|
// List literals implicitly assign integer keys
|
|
|
|
$array = ['One', 'Two', 'Three'];
|
|
|
|
echo $array[0]; // => "One"
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Output
|
|
|
|
*/
|
2013-06-27 20:49:03 +04:00
|
|
|
|
|
|
|
echo('Hello World!');
|
|
|
|
// Prints Hello World! to stdout.
|
|
|
|
// Stdout is the web page if running in a browser.
|
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
print('Hello World!'); // The same as echo
|
2013-06-27 20:49:03 +04:00
|
|
|
|
|
|
|
// echo is actually a language construct, so you can drop the parentheses.
|
|
|
|
echo 'Hello World!';
|
2013-06-28 00:49:58 +04:00
|
|
|
print 'Hello World!'; // So is print
|
2013-06-28 05:10:45 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
$paragraph = 'paragraph';
|
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
echo 100;
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $paragraph;
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-27 20:49:03 +04:00
|
|
|
// If short open tags are configured, or your PHP version is
|
|
|
|
// 5.4.0 or greater, you can use the short echo syntax
|
2013-06-30 01:34:54 +04:00
|
|
|
?>
|
|
|
|
<p><?= $paragraph ?></p>
|
2013-06-27 20:49:03 +04:00
|
|
|
<?php
|
|
|
|
|
2013-06-28 01:03:08 +04:00
|
|
|
$x = 1;
|
|
|
|
$y = 2;
|
|
|
|
$x = $y; // A now contains the same value sa $y
|
2013-06-30 01:34:54 +04:00
|
|
|
$z = &$y;
|
2013-06-28 05:10:45 +04:00
|
|
|
// $x now contains a reference to $y. Changing the value of
|
|
|
|
// $x will change the value of $y also, and vice-versa.
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $x; // => 2
|
|
|
|
echo $z; // => 2
|
|
|
|
$y = 0;
|
|
|
|
echo $x; // => 2
|
|
|
|
echo $z; // => 0
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Logic
|
|
|
|
*/
|
|
|
|
$a = 0;
|
|
|
|
$b = '0';
|
|
|
|
$c = '1';
|
|
|
|
$d = '1';
|
2013-06-28 01:03:08 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// assert throws a warning if its argument is not true
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// These comparisons will always be true, even if the types aren't the same.
|
|
|
|
assert($a == $b); // equality
|
|
|
|
assert($b != $a); // inequality
|
|
|
|
assert($a <> $b); // alternative inequality
|
|
|
|
assert($a < $c);
|
|
|
|
assert($c > $b);
|
|
|
|
assert($a <= $b);
|
|
|
|
assert($c >= $d);
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// The following will only be true if the values match and are the same type.
|
|
|
|
assert($c === $d);
|
|
|
|
assert($a !== $d);
|
|
|
|
assert(1 == '1');
|
|
|
|
assert(1 !== '1');
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Variables can be converted between types, depending on their usage.
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
$integer = 1;
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $integer + $integer; // => 2
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
$string = '1';
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $string + $string; // => 2 (strings are coerced to integers)
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
$string = 'one';
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $string + $string; // => 0
|
2013-06-27 20:49:03 +04:00
|
|
|
// Outputs 0 because the + operator cannot cast the string 'one' to a number
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Type casting can be used to treat a variable as another type
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
$boolean = (boolean) 1; // => true
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
$zero = 0;
|
2013-06-30 01:34:54 +04:00
|
|
|
$boolean = (boolean) $zero; // => false
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// There are also dedicated functions for casting most types
|
2013-06-27 13:42:07 +04:00
|
|
|
$integer = 5;
|
2013-06-27 20:49:03 +04:00
|
|
|
$string = strval($integer);
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
$var = null; // Null value
|
|
|
|
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Control Structures
|
|
|
|
*/
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
if (true) {
|
|
|
|
print 'I get printed';
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
if (false) {
|
|
|
|
print "I don't";
|
2013-06-27 13:42:07 +04:00
|
|
|
} else {
|
2013-06-30 01:34:54 +04:00
|
|
|
print 'I get printed';
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
if (false) {
|
|
|
|
print 'Does not get printed';
|
|
|
|
} elseif(true) {
|
|
|
|
print 'Does';
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
$x = 0;
|
|
|
|
if ($x === '0') {
|
|
|
|
print 'Does not print';
|
|
|
|
} elseif($x == '1') {
|
|
|
|
print 'Does not print';
|
2013-06-27 13:42:07 +04:00
|
|
|
} else {
|
2013-06-30 01:34:54 +04:00
|
|
|
print 'Does print';
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-30 01:34:54 +04:00
|
|
|
|
|
|
|
// This alternative syntax is useful for templates:
|
2013-06-27 20:49:03 +04:00
|
|
|
?>
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
<?php if ($x): ?>
|
2013-06-28 00:57:52 +04:00
|
|
|
This is displayed if the test is truthy.
|
2013-06-27 13:42:07 +04:00
|
|
|
<?php else: ?>
|
2013-06-28 00:57:52 +04:00
|
|
|
This is displayed otherwise.
|
2013-06-27 13:42:07 +04:00
|
|
|
<?php endif; ?>
|
|
|
|
|
2013-06-27 20:49:03 +04:00
|
|
|
<?php
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Use switch to save some logic.
|
|
|
|
switch ($x) {
|
|
|
|
case '0':
|
|
|
|
print 'Switch does type coercion';
|
|
|
|
break; // You must include a break, or you will fall through
|
2013-06-27 13:42:07 +04:00
|
|
|
case 'two':
|
|
|
|
case 'three':
|
2013-06-27 20:49:03 +04:00
|
|
|
// Do something if $variable is either 'two' or 'three'
|
2013-06-27 13:42:07 +04:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-27 20:49:03 +04:00
|
|
|
// Do something by default
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// While, do...while and for loops are probably familiar
|
2013-06-27 13:42:07 +04:00
|
|
|
$i = 0;
|
|
|
|
while ($i < 5) {
|
2013-06-27 20:49:03 +04:00
|
|
|
echo $i++;
|
2013-06-30 01:34:54 +04:00
|
|
|
}; // Prints "01234"
|
|
|
|
|
|
|
|
echo "\n";
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
do {
|
2013-06-27 20:49:03 +04:00
|
|
|
echo $i++;
|
2013-06-30 01:34:54 +04:00
|
|
|
} while ($i < 5); // Prints "01234"
|
|
|
|
|
|
|
|
echo "\n";
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
for ($x = 0; $x < 10; $x++) {
|
2013-06-27 20:49:03 +04:00
|
|
|
echo $x; // Will echo 0 - 9
|
2013-06-30 01:34:54 +04:00
|
|
|
}// Prints "0123456789"
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
$wheels = ['bicycle' => 2, 'car' => 4];
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Foreach loops can iterate over arrays
|
|
|
|
foreach ($wheels as $wheel_count){
|
|
|
|
echo "$wheel_count";
|
|
|
|
} // Prints "24"
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo "\n";
|
|
|
|
|
|
|
|
// You can iterate over the keys as well as the values
|
2013-06-27 13:42:07 +04:00
|
|
|
foreach ($wheels as $vehicle => $wheel_count) {
|
2013-06-27 20:49:03 +04:00
|
|
|
echo "A $vehicle has $wheel_count wheels";
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo "\n";
|
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
$i = 0;
|
|
|
|
while ($i < 5) {
|
2013-06-30 01:34:54 +04:00
|
|
|
if ($i === 3) {
|
|
|
|
break; // Exit out of the while loop
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-27 20:49:03 +04:00
|
|
|
echo $i++;
|
2013-06-30 01:34:54 +04:00
|
|
|
}// Prints "012"
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
for($i = 0; $i < 5; $i++){
|
|
|
|
if ($i === 3) {
|
2013-06-27 20:49:03 +04:00
|
|
|
continue; // Skip this iteration of the loop
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-30 01:34:54 +04:00
|
|
|
echo $i;
|
|
|
|
} // Prints "0124"
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Functions
|
|
|
|
*/
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Define a function with "function":
|
|
|
|
function my_function() {
|
|
|
|
return 'Hello';
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo my_function(); // => "Hello"
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// A valid function name starts with a letter or underscore, followed by any
|
|
|
|
// number of letters, numbers, or underscores.
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
function add($x, $y = 1) { // $y is optional, and defaults to 2
|
|
|
|
$result = $x + $y;
|
|
|
|
return $result;
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo add(4); // => 5
|
|
|
|
echo add(4, 2); // => 6
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// $result is not accessible outside the function
|
|
|
|
// print $result; // Gives a warning.
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Since PHP 5.3 you can declare anonymous functions;
|
|
|
|
$inc = function($x){
|
|
|
|
return $x + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
echo $inc(2); // => 3
|
2013-06-28 01:11:27 +04:00
|
|
|
|
|
|
|
function foo ($x, $y, $z) {
|
|
|
|
echo "$x - $y - $z";
|
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Functions can return functions
|
2013-06-28 01:11:27 +04:00
|
|
|
function bar ($x, $y) {
|
2013-06-30 01:34:54 +04:00
|
|
|
// Use 'use' to bring in outside variables
|
2013-06-28 01:11:27 +04:00
|
|
|
return function ($z) use ($x, $y) {
|
|
|
|
foo($x, $y, $z);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$bar = bar('A', 'B');
|
|
|
|
$bar('C');
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// You can call named functions using strings
|
|
|
|
$function_name = 'add';
|
|
|
|
echo $function_name(1, 2); // => 3
|
|
|
|
// But, you should probably use anonymous functions instead.
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Classes
|
|
|
|
*/
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
//Classes are defined with the class keyword
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
class MyClass {
|
2013-06-30 01:34:54 +04:00
|
|
|
const MY_CONST = 'value'; // A constant
|
|
|
|
static $staticVar = 'static';
|
|
|
|
public $property = 'public'; // Properties must declare their visibility
|
|
|
|
private $privprop = 'private'; // Accessible within the class only
|
|
|
|
protected $protprop = 'protected'; // Accessible within the class and subclasses
|
|
|
|
public $instanceProp;
|
|
|
|
|
|
|
|
// Create a constructor with __construct
|
|
|
|
public function __construct($instanceProp){
|
|
|
|
// Access instance variables with $this
|
|
|
|
$this->instanceProp = $instanceProp;
|
|
|
|
}
|
|
|
|
// Methods are declared as functions inside a class
|
|
|
|
public function myMethod() {
|
|
|
|
print "MyClass";
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-28 00:48:31 +04:00
|
|
|
|
2013-06-28 05:10:45 +04:00
|
|
|
final function youCannotOverrideMe() {
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-28 00:48:31 +04:00
|
|
|
|
2013-06-28 05:10:45 +04:00
|
|
|
public static function myStaticMethod() {
|
2013-06-30 01:34:54 +04:00
|
|
|
print "I am static";
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
echo MyClass::MY_CONST; // Outputs "value";
|
|
|
|
echo MyClass::$staticVar; // Outputs 'static';
|
|
|
|
MyClass::myStaticMethod(); // Outputs "I am static";
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Access class members using ->.
|
|
|
|
$my_class = new MyClass("An instance property"); // The parentheses are optional.
|
|
|
|
echo $my_class->property; // => "public"
|
|
|
|
echo $my_class->instanceProp; // => "An instance property"
|
|
|
|
$my_class->myMethod(); // => "MyClass"
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Extend classes using "extends"
|
|
|
|
class MyOtherClass extends MyClass{
|
|
|
|
function printProtectedProperty(){
|
|
|
|
echo $this->protprop;
|
|
|
|
}
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Override a method
|
|
|
|
function myMethod() {
|
|
|
|
parent::myMethod();
|
|
|
|
print " > MyOtherClass";
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
$my_other_class = new MyOtherClass("Instance prop");
|
|
|
|
$my_other_class->printProtectedProperty(); // => Prints "protected"
|
|
|
|
$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass"
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
final class YouCannotExtendMe {
|
|
|
|
}
|
|
|
|
|
|
|
|
// You can use "magic methods" to create getters and setters
|
|
|
|
class MyMapClass {
|
2013-06-27 20:49:03 +04:00
|
|
|
private $property;
|
2013-06-28 00:48:31 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
public function __get($key)
|
|
|
|
{
|
2013-06-27 20:49:03 +04:00
|
|
|
return $this->$key;
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
2013-06-28 00:48:31 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
public function __set($key, $value)
|
|
|
|
{
|
2013-06-27 20:49:03 +04:00
|
|
|
$this->$key = $value;
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
$x = new MyMapClass();
|
2013-06-27 20:49:03 +04:00
|
|
|
echo $x->property; // Will use the __get() method
|
|
|
|
$x->property = 'Something'; // Will use the __set() method
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Classes can be abstract (using the abstract keyword) or
|
|
|
|
// implement interfaces (using the implements keyword).
|
|
|
|
// An interface is declared with the interface keyword.
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
interface InterfaceOne
|
|
|
|
{
|
2013-06-27 20:49:03 +04:00
|
|
|
public function doSomething();
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
interface InterfaceTwo
|
|
|
|
{
|
2013-06-30 01:34:54 +04:00
|
|
|
public function doSomethingElse();
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class MyAbstractClass implements InterfaceOne
|
|
|
|
{
|
2013-06-30 01:34:54 +04:00
|
|
|
public $x = "doSomething";
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
|
2013-06-27 13:42:07 +04:00
|
|
|
{
|
2013-06-30 01:34:54 +04:00
|
|
|
public function doSomething(){
|
|
|
|
echo $x;
|
|
|
|
}
|
|
|
|
public function doSomethingElse(){
|
|
|
|
echo "doSomethingElse";
|
|
|
|
}
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
// Classes can implement more than one interface
|
|
|
|
class SomeOtherClass implements InterfaceOne, InterfaceTwo
|
|
|
|
{
|
2013-06-30 01:34:54 +04:00
|
|
|
public function doSomething(){
|
|
|
|
echo "doSomething";
|
|
|
|
}
|
|
|
|
public function doSomethingElse(){
|
|
|
|
echo "doSomethingElse";
|
|
|
|
}
|
2013-06-27 13:42:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
/********************************
|
|
|
|
* Traits
|
|
|
|
*/
|
2013-06-27 13:42:07 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
//Traits are available since PHP 5.4.0 and are declared using the trait keyword.
|
|
|
|
|
|
|
|
trait MyTrait {
|
|
|
|
public function myTraitMethod()
|
|
|
|
{
|
|
|
|
print "I have MyTrait";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyTraitfulClass
|
|
|
|
{
|
|
|
|
use MyTrait;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cls = new MyTraitfulClass();
|
|
|
|
$cls->myTraitMethod(); // Prints "I have MyTrait"
|
|
|
|
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
* Namespaces
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This section is separate, because a namespace declaration
|
|
|
|
// must be the first statement in a file. Let's pretend that is not the case
|
|
|
|
|
|
|
|
/*
|
|
|
|
```
|
2013-06-27 13:42:07 +04:00
|
|
|
```php
|
2013-06-27 20:49:03 +04:00
|
|
|
<?php
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// By default, classes exist in the global namespace, and can
|
|
|
|
// be explicitly called with a backslash.
|
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
$cls = new \MyClass();
|
|
|
|
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
|
|
|
|
// Set the namespace for a file
|
2013-06-27 13:42:07 +04:00
|
|
|
namespace My\Namespace;
|
|
|
|
|
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// (from another file)
|
2013-06-27 13:42:07 +04:00
|
|
|
$cls = new My\Namespace\MyClass;
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
//Or from within another namespace.
|
2013-06-27 13:42:07 +04:00
|
|
|
namespace My\Other\Namespace;
|
|
|
|
|
|
|
|
use My\Namespace\MyClass;
|
|
|
|
|
|
|
|
$cls = new MyClass();
|
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
// Or you can alias the namespace;
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-27 13:42:07 +04:00
|
|
|
namespace My\Other\Namespace;
|
|
|
|
|
|
|
|
use My\Namespace as SomeOtherNamespace;
|
|
|
|
|
|
|
|
$cls = new SomeOtherNamespace\MyClass();
|
2013-06-27 20:49:03 +04:00
|
|
|
|
2013-06-30 01:34:54 +04:00
|
|
|
*/
|
2013-06-27 13:42:07 +04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## More Information
|
|
|
|
|
|
|
|
Visit the [official PHP documentation](http://www.php.net/manual/) for reference and community input.
|
|
|
|
|
|
|
|
If you're interested in up-to-date best practices, visit [PHP The Right Way](http://www.phptherightway.com/).
|
|
|
|
|
2013-06-27 20:35:59 +04:00
|
|
|
If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/).
|