Dabbler Rookie Learn Web development-PHP Grammar Learning 1

Source: Internet
Author: User
Tags object serialization

1 indexed arrays

$fruit = Array ("Apple", "banana", "pineapple");

Print_r ($fruit);

There are three ways to initialize an indexed array:

1$arry[0] = "Apple";

2array (' 0 ' = "apple");

3array (' Apple '), the system starts from index 0 by default

4 Remove the value in the indexed array $fruit 0 = $fruit [' 0 '];

5for use, for use similar to C

Use of 5foreach

foreach ($fruit as $key = $value)

{

Echo ' first ' $key. ' The value is: '. $value;

}

2 Associative arrays

Associative array refers to an array of keys that are strings

$fruit = Array (' apple ' = ' apple ', ' orange ' = ' orange ')

Print_r ($fruit);

Creation of associative arrays

$arr = Array ();

$array [' apple '] = ' apple ';

Or

$arr = Array (' apple ' = ' apple ');

Take the value of the associative array $value = $fruit [' banana '];

foreach ($fruit as $key = $value)

Objects in 3PHP

1 Defining a class
Class Car {
var $name = ' car ';
function GetName () {
return $this->name;
}
}

$car = new car ();

$car->name = ' BMW ';

echo $car->getname ();

Variables in the same class have Public,private,protect

PHP uses __construct () to define a constructor in a class

Class Car {

function __construct ()

{

Print "constructor is called \ n";

}

function __destruct ()

{

The print "destructor is called \ n";

}

}

2 static properties and methods can be invoked without instantiating the class, directly using 类名::方法名 the method. Static properties do not allow an object to be called with an operator.

Static methods can also be dynamically called through variables

$func = ' getspeed ';

$classname = ' Car '

echo $className:: $func ();

Static methods cannot be called with this, and can be implemented with Self,parent Plus::

3 Overloading of methods

property is overloaded by using the __set,__get,__isset,_unset

ublic function __set ($key, $val) {        $this->ary[$key] = $val;    }        Public Function __get ($key) {        if (isset ($this->ary[$key]) {            return $this->ary[$key];        }        return null;    }

If the calling method does not exist, the __call method will be called, and if it is a static method, the __callstatic method will be called

Public Function __call ($name, $args) {        if ($name = = ' SpeedUp ') {            $this->speed + =;        }    }

4 advanced features for classes and objects

When all the properties of two instances of the same class are equal, you can use = = to determine

If the two variables of the same class are references to the same object, you can use = = = To determine

Object, copy an object with the keyword clone

Class Car {public    $name = ' car ';        Public Function __clone () {        $obj = new Car ();        $obj->name = $this->name;    }} $a = new car (), $a->name = ' new car '; $b = clone $a; var_dump ($b);

Object serialization, which can be serialized as a string by the Serialize method, used to store or pass data, and then deserialize the string into an object when needed by Unserialize.

$a = new Car (); $str = serialize ($a); The object is serialized into a string echo $str. ' <br> '; $b = Unserialize ($STR); Deserializes to Object Var_dump ($b);

Dabbler Rookie Learn Web development-PHP Grammar Learning 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.