PHP partial grammar

Keywords: PHP Attribute

array() creates an array:

1. Numeric arrays: arrays with numeric ID keys

2. Association Array: An array with a specified key that associates a value

3. Multidimensional arrays: arrays containing one or more arrays

$arr = array("Hello", "World");    // NUMERICAL ARRAY
$age = array("Peter" => "35", "Ben" => "37");    // Associative array
echo $age["Peter"];    // 35

 

Array method:

1. count($arr): Get length

2. foreach($array as $value): traverse arrays

$arr = array(1, 2, 3, 4, 5);
echo count($arr).PHP_EOL;    // Output length 5
foreach ($arr as $num)    // Output elements in the array in turn
    echo "$num".PHP_EOL;

3. sort($arr), rsort($arr): sort arrays in ascending and descending order

$arr = array(6, 7, 1, 4, 5);
sort($arr);    // Ascending order
foreach ($arr as $num)
    echo $num.PHP_EOL;
rsort($arr);    // Descending arrangement
foreach ($arr as $num)
    echo $num.PHP_EOL;

4. asort($arr), arsort($arr): sort in ascending and descending order according to the value of the associated array

$arr = array("1" => 3, "2" => 2, "3" => 1);
asort($arr);
foreach ($arr as $a)    // 1 2 3
    echo $a.PHP_EOL;
arsort($arr);
foreach ($arr as $a)    // 3 2 1
    echo $a.PHP_EOL;

5. ksort($arr), krsort($arr): Sort ascending and descending according to the key of the associated array

$arr = array("1" => 3, "2" => 2, "3" => 1);
ksort($arr);
foreach ($arr as $a)    // 3 2 1
    echo $a.PHP_EOL;
krsort($arr);
foreach ($arr as $a)    // 1 2 3
    echo $a.PHP_EOL;

 

Super global variables:

1. $GLOBALS: A globally combined array of all variables, whose name is the key of the array

2, $_SERVER: An array containing information such as header information, path, and script location

$x = 15;
function test() {
    $GLOBALS["x"] = 5;
}
test();
echo $x.PHP_EOL;    // 5
echo $_SERVER["PHP_SELF"];    // Current execution script file name

3, $_REQUEST: Used to collect data submitted by HTML forms

4, $_POST: Widely used to collect form data, specifying post in the form tag of HTML

5, $_GET: Widely used to collect form data with the form attribute "method = get"

 

Class:

1. Use class keyword followed by class name definition

2. Define variables and methods in a pair of braces after the class name

3. Variables of classes are declared by var, and variables can also initialize values.

4. Function definitions are similar to those of PHP functions, but functions can only be accessed through this class and its instantiated objects.

5. The variable $this represents its own object, and PHP_EOL is a newline character

6. Use new to instantiate a class object

7. Constructor: function construct ([mixed $args [, $...])

8. Destructor: function destruct (void)

9. Using extends to inherit a class, PHP does not support multiple inheritance

10. We can rewrite methods inherited from parent classes to meet the needs of subclasses

11. We can add keywords of access control in front of attributes or methods to achieve access control, which defaults to public.

12. PHP does not automatically call the parent class's constructor in the child class's constructor

13. To execute the construction method of the parent class, you need to call parent:_construct() in the construction method of the child class.

14. If the subclass does not define a constructor, it will inherit the parent class's

class Greeter {
    var $name;
    function __construct($your_name) {
        $this->$name = $your_name;
        echo "<p>In Greeter</p>";
    }
    function say() {
        return "Hello ".$this->$name."!";
    }
}
$greet = new Greeter("Lemon");    // In Greeter
echo $greet->say();    // Hello Lemon!
class Say extends Greeter {
    function __construct() {
        echo "<p>In Say</p>";
        parent::__construct("Yam");
    }
}
$say = new Say();    // In Say    In Greeter
echo $say->say();    // Hello Yam!

Note: Since PHP 5.3.0, a variable can be used to call the class dynamically, but the value of the variable cannot be a keyword.

The invariable value in a class can be defined as a constant, which must be a fixed value. It is unnecessary to define and use it.$

 

Interface:

1. Use interface to define interfaces (specify which methods a class needs to implement, but do not need to define them)

2. All methods must be publicly owned

3. implements are used to implement all the methods in the interface. Multiple interfaces can be implemented, and the interfaces are separated by commas.

interface test {
    public function get_name();
    public function set_name($name);
}
class information implements test {
    var $name;
    function __construct($your_name) {
        $this->$name = $your_name;
    }
        // If one of the following methods is not implemented, an error will be reported.
    function get_name() {
        return $this->$name;
    }
    function set_name($your_name) {
        $this->$name = $your_name;
    }
}
$info = new information("Lemon");
echo $info->get_name().PHP_EOL;    // Lemon
$info->set_name("Yam");
echo $info->get_name();    // Yam

 

Abstract classes:

1. Any class must be abstract if at least one of its methods is abstract.

2. The extraction class cannot be instantiated (abstract)

3. When inheriting an abstract class, the subclass must define all abstract methods of the abstract parent class, and access control must be the same or more relaxed as that of the parent class.

abstract class AbstractClass
{
 // Force subclasses to define these methods
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);
    // General Method (Non-abstract Method)
    public function printOut() {
        print $this->getValue() . PHP_EOL;
    }
}
class ConcreteClass extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass";
    }
    // More relaxed than before
    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass";
    }
}
$class1 = new ConcreteClass;
$class1->printOut();
echo $class1->prefixValue('FOO_') . PHP_EOL;

 

static keyword:

1. Declaring class attributes or methods as static allows direct access without instantiation

2. Static attributes cannot be accessed through a class of instantiated objects, but static methods can

3. The pseudovariable $this is unavailable because static methods can be invoked without the need for objects.

4. Static methods cannot be accessed by objects through - > operators

class Test {
    public static $num = 1;
    public function add_num() {
        echo ++self::$num.PHP_EOL;
    }
}
echo Test::$num.PHP_EOL;   // 1
$test = new Test();
$test->add_num();    // 2
$test->add_num();    // 3
$test = new Test();
$test->add_num();    // 4 

 

The final keyword:

1. If the method in the parent class is declared final, the subclass cannot override the method.

2. If a class is declared final, it cannot be inherited

Posted by Call-911 on Sat, 03 Aug 2019 07:46:30 -0700