PHP Basic finishing 1

Source: Internet
Author: User

Array

PHP has two types of arrays: indexed arrays, associative arrays, indexes and associations two words are for the keys of an array, index subscript starting from 0

Initialization
    1. Indexed array
    • Create an empty array$T = array();
    • Initializes an array of$students = array("mac","windows");
    • Print out the entire arrayprint_r($students);
    1. Associative arrays
    • Initializing an array
$fruit = array(    ‘apple‘=>"苹果",    ‘banana‘=>"香蕉",    
Assign value
    • Indexed arrays have three ways to assign values
$arr[0] = ‘matrix‘;array(‘0‘=>‘matrix‘);array(‘matrix‘); //这相当于array(‘0‘=>‘matrix‘)///如果存在打印$arr=array(‘matrix‘);if(isset($arr)){print_r($arr);}
    • Associative array assignment is the first string (must be a string) when the index, do not repeat the
$arr[‘apple‘]=‘苹果‘
foreach Access array
$fruit=array(‘a‘,‘b‘,‘c‘);foreach($fruit as $k=>$v){echo ‘<br>第‘.$k.‘个值是‘.$v;//  .是连接符}$os=array(‘windows‘=>1,‘mac‘=>2);foreach($os as $k=>$t){echo ‘<br>os:‘.$k.‘ number:‘.$t;}
Classes and objects

Class is the basic concept of object-oriented programming, learn about the oop of PHP

Get a class and an instance
class Car{$name = ‘汽车‘;function getName(){return $this->name;//方法内部可以使用$this伪变量调用对象的属性或者方法}}$car = new Car();echo $car->getName();
Properties of the class

Variables defined in a class are called properties, and usually properties are associated with fields in the database, so they can also be called fields. A property declaration is made up of the keyword public,protected or private, followed by a common variable declaration. A variable of the property can be set to the default value of the initialization, and the default value must be constant.

    • Directly on the code and comment on the understanding
class Car{//公共属性public $name = ‘汽车‘;//受保护属性,可以被自身和其子类,父类访问protected $color = ‘白色‘;//私有属性,只能在定义类的位置访问privcate $price = ‘6666666‘;}
    • Without attributes, the default is public, and object operators generally access the properties or methods of an object. Static properties are accessed using:: Double colons. The properties of the current object are called with $this pseudo-variables inside the class member method.
$car=new Car();echo $car->name;echo $car->color; //报错echo $car->price; //报错
Methods of the class

Process-oriented function called functions, and object-oriented function called methods. There are also three ways to modify the method: Public,protected,privite. A static method that can be called without instantiation, with a static modifier and a double colon for the operator

Example code:

class Car {    public function getName() {        return ‘汽车‘;    }?}$car = new Car();echo $car->getName();
class Car {    public static function getName() {        return ‘汽车‘;    }?}echo Car::getName(); //结果为“汽车”
Constructors and destructors

PHP can use __construct () in a class to define a constructor, a class with constructors that will call the function every time the object is created, so it is often used to do some initialization work when the object is created. Note: There are two underscores in front of construct ()

code example

class Car{function __construct(){print "构造函数被调用了\n";}}$car=new Car();//此时就会调用构造函数,输出一个字符串

If _construct is defined in a subclass, the constructor of the parent class is not called, and if the constructor of the parent class needs to be called, the parent::_construct () is required.

class Car{function __construct(){print "parent";}}class Truck extends Car{function __construct(){print "child";parent::__construct();}}$car = new Truck();

Similarly, PHP supports destructors, which are defined using __destruct (), which refers to functions that are executed when all references to an object are deleted, or when an object is explicitly destroyed.

Look at the code, you know.

class Car{function __construct(){print "构造函数调用了\n";    }    function __destruct(){    print "析构函数调用了\n";    }}$car = new Car();//实例化时调用了__construct()echo ‘下面是销毁‘;unset($car); //销毁时调用了__destruct()//php代码执行完时会自动回收和销毁对象,因此一般情况下不用自己手动去显示销毁对象
Static keyword

static properties and static methods can be called without instantiation

    • 类名::方法名Calling static properties directly, not allowing call-to-use, $this is not allowed in static methods, and static methods and properties can be called internally using Self,parent,static.
class Car{private static $speed =10;public static function getSpeed(){return self::$speed;    }    public static function speedUp(){return self::$seed+=10;}}class BigCar extends Car{public static function start(){    parent::speedUp();    }}BigCar::start();echo BigCar::getSpeed();
    • Static methods can also be called by variables.
$func = ‘getSpeed‘;$className = ‘Car‘;echo $classNme::$func();
Access control
    • Public: can be accessed anywhere
    • Protected: itself, subclasses and parent classes can access
    • Private: Only the definition itself can be accessed

Property must define access control, the class does not have write access control and the default is public

class Car{$speed = 10;//报错,无访问控制public $name;}

If the constructor is defined as a private method, it is not allowed to instantiate directly, and to instantiate it with a static method, it is often used in design mode to control the creation of objects, such as a singleton pattern that only allows a globally unique variable

Examples are as follows

class Car{private function __construct(){echo ‘object create‘;}private static $_object = null;public static function getInstance(){if(empty(self::$object)){self::$_object = new Car();//定义处可以调用私有方法,因此可以创建对象}return self::$_object;}}//$car = new Car();此处如果实例化就会报错$car=Car::getInstance();//通过静态方法来获得实例
Advanced features of the object

Object comparison, when all the properties of two instances of the same class are equal, you can use the comparison operator = = to make judgments, and when you need to determine whether two variables are references of the same object, you can use the congruent operator = = = to make judgments. Not to be continued

PHP Basic finishing 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.