各种类及接口

目录

无法归入其它 SPL 分类的类和接口。

简介

This class allows objects to work as arrays.

类摘要

ArrayObject

class ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable {

/* 常量 */

const integer STD_PROP_LIST = 1 ;

const integer ARRAY_AS_PROPS = 2 ;

/* 方法 */

public __construct ([ mixed $input = array() [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )

public void append ( mixed $value )

public void asort ( void )

public int count ( void )

public array exchangeArray ( mixed $input )

public array getArrayCopy ( void )

public int getFlags ( void )

public ArrayIterator getIterator ( void )

public string getIteratorClass ( void )

public void ksort ( void )

public void natcasesort ( void )

public void natsort ( void )

public bool offsetExists ( mixed $index )

public mixed offsetGet ( mixed $index )

public void offsetSet ( mixed $index , mixed $newval )

public void offsetUnset ( mixed $index )

public string serialize ( void )

public void setFlags ( int $flags )

public void setIteratorClass ( string $iterator_class )

public void uasort ( callable $cmp_function )

public void uksort ( callable $cmp_function )

public void unserialize ( string $serialized )

}

预定义常量

ArrayObject Flags

ArrayObject::STD_PROP_LIST
Properties of the object have their normal functionality when accessed as list (var_dump, foreach, etc.).

ArrayObject::ARRAY_AS_PROPS
Entries can be accessed as properties (read and write).

更新日志

版本说明
5.3.0Implements Serializable.

ArrayObject::append

追加新的值作为最后一个元素。

说明

public void ArrayObject::append ( mixed $value )

追加新的值作为最后一个元素。

Note:

ArrayObject 从 object 初始化时不能调用此方法。使用 class="methodname">ArrayObject::offsetSet 代替。

参数

value
将要被追加的值

返回值

没有返回值。

范例

示例 #1 ArrayObject::append 例子

<?php
$arrayobj = new ArrayObject(array('first','second','third'));
$arrayobj->append('fourth');
$arrayobj->append(array('five', 'six'));
var_dump($arrayobj);
?>

以上例程会输出:

object(ArrayObject)#1 (5) {
  [0]=>
  string(5) "first"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "third"
  [3]=>
  string(6) "fourth"
  [4]=>
  array(2) {
    [0]=>
    string(4) "five"
    [1]=>
    string(3) "six"
  }
}

参见

  • ArrayObject::offsetSet

ArrayObject::asort

Sort the entries by value

说明

public void ArrayObject::asort ( void )

Sorts the entries such that the keys maintain their correlation with the entries they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayObject::asort example

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->asort();

foreach ($fruitArrayObject as $key => $val) {
    echo "$key = $val\n";
}
?>

以上例程会输出:

c = apple
b = banana
d = lemon
a = orange

The fruits have been sorted in alphabetical order, and the key associated with each entry has been maintained.

参见

  • ArrayObject::ksort
  • ArrayObject::natsort
  • ArrayObject::natcasesort
  • ArrayObject::uasort
  • ArrayObject::uksort

ArrayObject::__construct

Construct a new array object

说明

public ArrayObject::__construct ([ mixed $input = array() [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] )

This constructs a new array object.

参数

input
The input parameter accepts an array or an Object.

flags
Flags to control the behaviour of the ArrayObject object. See ArrayObject::setFlags.

iterator_class
Specify the class that will be used for iteration of the ArrayObject object.

返回值

Returns an ArrayObject object on success.

错误/异常

Throws InvalidArgumentException when:

  • input is not an array or object
  • flags is not an integer
  • iterator_class is not an object that implements Iterator

范例

示例 #1 ArrayObject::__construct example

<?php
$array = array('1' => 'one',
               '2' => 'two',
               '3' => 'three');

$arrayobject = new ArrayObject($array);

var_dump($arrayobject);
?>

以上例程会输出:

object(ArrayObject)#1 (3) {
  [1]=>
  string(3) "one"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
}

参见

  • ArrayObject::setflags

ArrayObject::count

统计 ArrayObject 内 public 属性的数量

说明

public int ArrayObject::count ( void )

获取 ArrayObject 的 public 属性数量

参数

此函数没有参数。

返回值

对象 ArrayObject 的 public 属性数量

Note:

当对象 ArrayObject 是从数组构造而来时,所有属性都是 public 的。

范例

示例 #1 ArrayObject::count 例子

<?php
class Example {
    public $public = 'prop:public';
    private $prv   = 'prop:private';
    protected $prt = 'prop:protected';
}

$arrayobj = new ArrayObject(new Example());
var_dump($arrayobj->count());

$arrayobj = new ArrayObject(array('first','second','third'));
var_dump($arrayobj->count());
?>

以上例程会输出:

int(1)
int(3)

ArrayObject::exchangeArray

Exchange the array for another one

说明

public array ArrayObject::exchangeArray ( mixed $input )

Exchange the current array with another array or object.

参数

input
The new array or object to exchange with the current array.

返回值

Returns the old array.

范例

示例 #1 ArrayObject::exchangeArray example

<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
// Array of locations in Europe
$locations = array('Amsterdam', 'Paris', 'London');

$fruitsArrayObject = new ArrayObject($fruits);

// Now exchange fruits for locations
$old = $fruitsArrayObject->exchangeArray($locations);
print_r($old);
print_r($fruitsArrayObject);

?>

以上例程会输出:

Array
(
    [lemons] => 1
    [oranges] => 4
    [bananas] => 5
    [apples] => 10
)
ArrayObject Object
(
    [0] => Amsterdam
    [1] => Paris
    [2] => London
)

ArrayObject::getArrayCopy

Creates a copy of the ArrayObject

说明

public array ArrayObject::getArrayCopy ( void )

Exports the ArrayObject to an array.

参数

此函数没有参数。

返回值

Returns a copy of the array. When the ArrayObject refers to an object, an array of the public properties of that object will be returned.

范例

示例 #1 ArrayObject::getArrayCopy example

<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);
$fruitsArrayObject['pears'] = 4;

// create a copy of the array
$copy = $fruitsArrayObject->getArrayCopy();
print_r($copy);

?>

以上例程会输出:

Array
(
    [lemons] => 1
    [oranges] => 4
    [bananas] => 5
    [apples] => 10
    [pears] => 4
)

ArrayObject::getFlags

Gets the behavior flags

说明

public int ArrayObject::getFlags ( void )

Gets the behavior flags of the ArrayObject. See the ArrayObject::setFlags method for a list of the available flags.

参数

此函数没有参数。

返回值

Returns the behavior flags of the ArrayObject.

范例

示例 #1 ArrayObject::getFlags example

<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);

// Get the current flags
$flags = $fruitsArrayObject->getFlags();
var_dump($flags);

// Set new flags
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);

// Get the new flags
$flags = $fruitsArrayObject->getFlags();
var_dump($flags);
?>

以上例程会输出:

int(0)
int(2)

参见

  • ArrayObject::setFlags

ArrayObject::getIterator

Create a new iterator from an ArrayObject instance

说明

public ArrayIterator ArrayObject::getIterator ( void )

Create a new iterator from an ArrayObject instance.

参数

此函数没有参数。

返回值

An iterator from an ArrayObject.

范例

示例 #1 ArrayObject::getIterator example

<?php
$array = array('1' => 'one',
               '2' => 'two',
               '3' => 'three');

$arrayobject = new ArrayObject($array);

$iterator = $arrayobject->getIterator();

while($iterator->valid()) {
    echo $iterator->key() . ' => ' . $iterator->current() . "\n";

    $iterator->next();
}
?>

以上例程会输出:

1 => one
2 => two
3 => three

ArrayObject::getIteratorClass

Gets the iterator classname for the ArrayObject

说明

public string ArrayObject::getIteratorClass ( void )

Gets the class name of the array iterator that is used by ArrayObject::getIterator().

参数

此函数没有参数。

返回值

Returns the iterator class name that is used to iterate over this object.

范例

示例 #1 ArrayObject::getIteratorClass example

<?php
// Custom ArrayIterator (inherits from ArrayIterator)
class MyArrayIterator extends ArrayIterator {
    // custom implementation
}

// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);

// Get the current class name
$className = $fruitsArrayObject->getIteratorClass();
var_dump($className);

// Set new classname
$fruitsArrayObject->setIteratorClass('MyArrayIterator');

// Get the new iterator classname
$className = $fruitsArrayObject->getIteratorClass();
var_dump($className);
?>

以上例程会输出:

string(13) "ArrayIterator"
string(15) "MyArrayIterator"

参见

ArrayObject::ksort

Sort the entries by key

说明

public void ArrayObject::ksort ( void )

Sorts the entries by key, maintaining key to entry correlations. This is useful mainly for associative arrays.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayObject::ksort example

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->ksort();

foreach ($fruitArrayObject as $key => $val) {
    echo "$key = $val\n";
}
 ?>

以上例程会输出:

a = orange
b = banana
c = apple
d = lemon

参见

  • ArrayObject::asort
  • ArrayObject::natsort
  • ArrayObject::natcasesort
  • ArrayObject::uasort
  • ArrayObject::uksort

ArrayObject::natcasesort

Sort an array using a case insensitive "natural order" algorithm

说明

public void ArrayObject::natcasesort ( void )

This method is a case insensitive version of ArrayObject::natsort.

This method implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayObject::natcasesort example

<?php
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');

$arr1 = new ArrayObject($array);
$arr2 = clone $arr1;

$arr1->asort();
echo "Standard sorting\n";
print_r($arr1);

$arr2->natcasesort();
echo "\nNatural order sorting (case-insensitive)\n";
print_r($arr2);
?>

以上例程会输出:

Standard sorting
ArrayObject Object
(
    [0] => IMG0.png
    [5] => IMG3.png
    [4] => img1.png
    [2] => img10.png
    [1] => img12.png
    [3] => img2.png
)

Natural order sorting (case-insensitive)
ArrayObject Object
(
    [0] => IMG0.png
    [4] => img1.png
    [3] => img2.png
    [5] => IMG3.png
    [2] => img10.png
    [1] => img12.png
)

For more information see: Martin Pool's » Natural Order String Comparison page.

参见

  • ArrayObject::asort
  • ArrayObject::ksort
  • ArrayObject::natsort
  • ArrayObject::uasort
  • ArrayObject::uksort

ArrayObject::natsort

Sort entries using a "natural order" algorithm

说明

public void ArrayObject::natsort ( void )

This method implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in ArrayObject::asort) method can be seen in the example below.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayObject::natsort example

<?php
$array = array("img12.png", "img10.png", "img2.png", "img1.png");

$arr1 = new ArrayObject($array);
$arr2 = clone $arr1;

$arr1->asort();
echo "Standard sorting\n";
print_r($arr1);

$arr2->natsort();
echo "\nNatural order sorting\n";
print_r($arr2);
?>

以上例程会输出:

Standard sorting
ArrayObject Object
(
    [3] => img1.png
    [1] => img10.png
    [0] => img12.png
    [2] => img2.png
)

Natural order sorting
ArrayObject Object
(
    [3] => img1.png
    [2] => img2.png
    [1] => img10.png
    [0] => img12.png
)

For more information see: Martin Pool's » Natural Order String Comparison page.

参见

  • ArrayObject::asort
  • ArrayObject::ksort
  • ArrayObject::natcasesort
  • ArrayObject::uasort
  • ArrayObject::uksort

ArrayObject::offsetExists

Returns whether the requested index exists

说明

public bool ArrayObject::offsetExists ( mixed $index )

参数

index
The index being checked.

返回值

TRUE if the requested index exists, otherwise FALSE

范例

示例 #1 ArrayObject::offsetExists example

<?php
$arrayobj = new ArrayObject(array('zero', 'one', 'example'=>'e.g.'));
var_dump($arrayobj->offsetExists(1));
var_dump($arrayobj->offsetExists('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>

以上例程会输出:

bool(true)
bool(true)
bool(false)

ArrayObject::offsetGet

Returns the value at the specified index

说明

public mixed ArrayObject::offsetGet ( mixed $index )

参数

index
The index with the value.

返回值

The value at the specified index or NULL.

错误/异常

Produces an E_NOTICE error message when the specified index does not exist.

范例

示例 #1 ArrayObject::offsetGet example

<?php
$arrayobj = new ArrayObject(array('zero', 7, 'example'=>'e.g.'));
var_dump($arrayobj->offsetGet(1));
var_dump($arrayobj->offsetGet('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>

以上例程会输出:

int(7)
string(4) "e.g."
bool(false)

ArrayObject::offsetSet

为指定索引设定新的值

说明

public void ArrayObject::offsetSet ( mixed $index , mixed $newval )

设置指定的索引为新的值

参数

index
将要被设置的索引

newval
参数 index 所对应的新值。

返回值

没有返回值。

范例

示例 #1 ArrayObject::offsetSet 例子

<?php
class Example {
    public $property = 'prop:public';
}
$arrayobj = new ArrayObject(new Example());
$arrayobj->offsetSet(4, 'four');
$arrayobj->offsetSet('group', array('g1', 'g2'));
var_dump($arrayobj);

$arrayobj = new ArrayObject(array('zero','one'));
$arrayobj->offsetSet(null, 'last');
var_dump($arrayobj);
?>

以上例程会输出:

object(ArrayObject)#1 (3) {
  ["property"]=>
  string(11) "prop:public"
  [4]=>
  string(4) "four"
  ["group"]=>
  array(2) {
    [0]=>
    string(2) "g1"
    [1]=>
    string(2) "g2"
  }
}
object(ArrayObject)#3 (3) {
  [0]=>
  string(4) "zero"
  [1]=>
  string(3) "one"
  [2]=>
  string(4) "last"
}

参见

  • ArrayObject::append

ArrayObject::offsetUnset

Unsets the value at the specified index

说明

public void ArrayObject::offsetUnset ( mixed $index )

Unsets the value at the specified index.

参数

index
The index being unset.

返回值

没有返回值。

范例

示例 #1 ArrayObject::offsetUnset example

<?php
$arrayobj = new ArrayObject(array(0=>'zero',2=>'two'));
$arrayobj->offsetUnset(2);
var_dump($arrayobj);
?>

以上例程会输出:

object(ArrayObject)#1 (1) {
  [0]=>
  string(4) "zero"
}

ArrayObject::serialize

Serialize an ArrayObject

说明

public string ArrayObject::serialize ( void )

Serializes an ArrayObject.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The serialized representation of the ArrayObject.

范例

示例 #1 ArrayObject::serialize example

<?php
$o = new ArrayObject();

$s1 = serialize($o);
$s2 = $o->serialize();

var_dump($s1);
var_dump($s2);
?>

以上例程会输出:

string(45) "C:11:"ArrayObject":21:{x:i:0;a:0:{};m:a:0:{}}"
string(21) "x:i:0;a:0:{};m:a:0:{}"

参见

ArrayObject::setFlags

Sets the behavior flags

说明

public void ArrayObject::setFlags ( int $flags )

Set the flags that change the behavior of the ArrayObject.

参数

flags
The new ArrayObject behavior. It takes on either a bitmask, or named constants. Using named constants is strongly encouraged to ensure compatibility for future versions.

The available behavior flags are listed below. The actual meanings of these flags are described in the predefined constants.

valueconstant
1ArrayObject::STD_PROP_LIST
2ArrayObject::ARRAY_AS_PROPS

返回值

没有返回值。

范例

示例 #1 ArrayObject::setFlags example

<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);

// Try to use array key as property
var_dump($fruitsArrayObject->lemons);
// Set the flag so that the array keys can be used as properties of the ArrayObject
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
// Try it again
var_dump($fruitsArrayObject->lemons);
?>

以上例程会输出:

NULL
int(1)

ArrayObject::setIteratorClass

Sets the iterator classname for the ArrayObject

说明

public void ArrayObject::setIteratorClass ( string $iterator_class )

Sets the classname of the array iterator that is used by ArrayObject::getIterator().

参数

iterator_class
The classname of the array iterator to use when iterating over this object.

返回值

没有返回值。

范例

示例 #1 ArrayObject::setIteratorClass example

<?php
// Custom ArrayIterator (inherits from ArrayIterator)
class MyArrayIterator extends ArrayIterator {
    // custom implementation
}

// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);

$fruitsArrayObject = new ArrayObject($fruits);

// Set the iterator classname to the newly
$fruitsArrayObject->setIteratorClass('MyArrayIterator');
print_r($fruitsArrayObject->getIterator());

?>

以上例程会输出:

MyArrayIterator Object
(
    [lemons] => 1
    [oranges] => 4
    [bananas] => 5
    [apples] => 10
)

ArrayObject::uasort

Sort the entries with a user-defined comparison function and maintain key association

说明

public void ArrayObject::uasort ( callable $cmp_function )

This function sorts the entries such that keys maintain their correlation with the entry that they are associated with, using a user-defined comparison function.

This is used mainly when sorting associative arrays where the actual element order is significant.

参数

cmp_function
Function cmp_function should accept two parameters which will be filled by pairs of entries. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

返回值

没有返回值。

范例

示例 #1 ArrayObject::uasort example

<?php
// Comparison function
function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$arrayObject = new ArrayObject($array);
print_r($arrayObject);

// Sort and print the resulting array
$arrayObject->uasort('cmp');
print_r($arrayObject);
?>

以上例程会输出:

Array
(
    [a] => 4
    [b] => 8
    [c] => -1
    [d] => -9
    [e] => 2
    [f] => 5
    [g] => 3
    [h] => -4
)
Array
(
    [d] => -9
    [h] => -4
    [c] => -1
    [e] => 2
    [g] => 3
    [a] => 4
    [f] => 5
    [b] => 8
)

参见

  • ArrayObject::asort
  • ArrayObject::ksort
  • ArrayObject::natsort
  • ArrayObject::natcasesort
  • ArrayObject::uksort

ArrayObject::uksort

Sort the entries by keys using a user-defined comparison function

说明

public void ArrayObject::uksort ( callable $cmp_function )

This function sorts the keys of the entries using a user-supplied comparison function. The key to entry correlations will be maintained.

参数

cmp_function
The callback comparison function.

Function cmp_function should accept two parameters which will be filled by pairs of entry keys. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

返回值

没有返回值。

范例

示例 #1 ArrayObject::uksort example

<?php
function cmp($a, $b) {
    $a = preg_replace('@^(a|an|the) @', '', $a);
    $b = preg_replace('@^(a|an|the) @', '', $b);
    return strcasecmp($a, $b);
}

$array = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
$arrayObject = new ArrayObject($array);
$arrayObject->uksort('cmp');

foreach ($arrayObject as $key => $value) {
    echo "$key: $value\n";
}
?>

以上例程会输出:

an apple: 3
a banana: 4
the Earth: 2
John: 1

参见

  • ArrayObject::asort
  • ArrayObject::ksort
  • ArrayObject::natsort
  • ArrayObject::natcasesort
  • ArrayObject::uasort

ArrayObject::unserialize

Unserialize an ArrayObject

说明

public void ArrayObject::unserialize ( string $serialized )

Unserializes a serialized ArrayObject.

Warning

本函数还未编写文档,仅有参数列表。

参数

serialized
The serialized ArrayObject.

返回值

The unserialized ArrayObject.

参见

简介

The SplObserver interface is used alongside SplSubject to implement the Observer Design Pattern.

接口摘要

SplObserver

class SplObserver {

/* 方法 */

abstract public void update ( SplSubject $subject )

}

SplObserver::update

Receive update from subject

说明

abstract public void SplObserver::update ( SplSubject $subject )

This method is called when any SplSubject to which the observer is attached calls SplSubject::notify.

Warning

本函数还未编写文档,仅有参数列表。

参数

subject
The SplSubject notifying the observer of an update.

返回值

没有返回值。

简介

The SplSubject interface is used alongside SplObserver to implement the Observer Design Pattern.

接口摘要

SplSubject

class SplSubject {

/* 方法 */

abstract public void attach ( SplObserver $observer )

abstract public void detach ( SplObserver $observer )

abstract public void notify ( void )

}

SplSubject::attach

Attach an SplObserver

说明

abstract public void SplSubject::attach ( SplObserver $observer )

Attaches an SplObserver so that it can be notified of updates.

Warning

本函数还未编写文档,仅有参数列表。

参数

observer
The SplObserver to attach.

返回值

没有返回值。

SplSubject::detach

Detach an observer

说明

abstract public void SplSubject::detach ( SplObserver $observer )

Detaches an observer from the subject to no longer notify it of updates.

Warning

本函数还未编写文档,仅有参数列表。

参数

observer
The SplObserver to detach.

返回值

没有返回值。

SplSubject::notify

Notify an observer

说明

abstract public void SplSubject::notify ( void )

Notifies all attached observers.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。