uopz_add_function

Adds non-existent function or method

说明

bool uopz_add_function ( string $function , Closure $handler [, int &$flags = ZEND_ACC_PUBLIC ] )

bool uopz_add_function ( string $class , string $function , Closure $handler [, int &$flags = ZEND_ACC_PUBLIC [, int &$all = TRUE ]] )

Adds a non-existent function or method.

参数

class
The name of the class.

function
The name of the function or method.

handler
The Closure that defines the new function or method.

flags
Flags to set for the new function or method.

all
Whether all classes that descend from class will also be affected.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

错误/异常

uopz_add_function throws a RuntimeException if the function or method to add already exists.

范例

示例 #1 Basic uopz_add_function Usage

<?php
uopz_add_function('foo', function () {echo 'bar';});
foo();
?>

以上例程会输出:

bar

参见

  • uopz_del_function
  • uopz_set_return

uopz_allow_exit

Allows control over disabled exit opcode

说明

void uopz_allow_exit ( bool $allow )

By default uopz disables the exit opcode, so exit calls are practically ignored. uopz_allow_exit allows to control this behavior.

参数

allow
Whether to allow the execution of exit opcodes or not.

返回值

没有返回值。

范例

示例 #1 uopz_allow_exit example

<?php
exit(1);
echo 1;
uopz_allow_exit(true);
exit(2);
echo 2;
?>

以上例程会输出:

1

注释

Caution

OPcache optimizes away dead code after unconditional exit.

参见

  • uopz_get_exit_status

uopz_backup

Backup a function

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_backup ( string $function )

void uopz_backup ( string $class , string $function )

Backup a function at runtime, to be restored on shutdown

参数

class
The name of the class containing the function to backup

function
The name of the function

返回值

范例

示例 #1 uopz_backup example

<?php
uopz_backup("fgets");
uopz_function("fgets", function(){
    return true;
});
var_dump(fgets());
?>

以上例程会输出:

bool(true)

uopz_compose

Compose a class

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_compose ( string $name , array $classes [, array $methods [, array $properties [, int $flags ]]] )

Creates a new class of the given name that implements, extends, or uses all of the provided classes

参数

name
A legal class name

classes
An array of class, interface and trait names

methods
An associative array of methods, values are either closures or [modifiers => closure]

properties
An associative array of properties, keys are names, values are modifiers

flags
Entry type, by default ZEND_ACC_CLASS

返回值

范例

示例 #1 uopz_compose example

<?php
class myClass {}
trait myTrait {}
interface myInterface {}

uopz_compose(
    Composed::class, [
        myClass::class, 
        myTrait::class, 
        myInterface::class
    ], [
    "__construct" => function() {
        /* ... */
    }
]);

var_dump(
 class_uses(Composed::class),
 class_parents(Composed::class),
 class_implements(Composed::class));
?>

以上例程会输出:

array(1) {
  ["myTrait"]=>
  string(7) "myTrait"
}
array(1) {
  ["myClass"]=>
  string(7) "myClass"
}
array(1) {
  ["myInterface"]=>
  string(11) "myInterface"
}

uopz_copy

Copy a function

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

Closure uopz_copy ( string $function )

Closure uopz_copy ( string $class , string $function )

Copy a function by name

参数

class
The name of the class containing the function to copy

function
The name of the function

返回值

A Closure for the specified function

范例

示例 #1 uopz_copy example

<?php
$strtotime = uopz_copy('strtotime');

uopz_function("strtotime", function($arg1, $arg2) use($strtotime) {
    /* can call original strtotime from here */
    var_dump($arg1);
});

var_dump(strtotime('dummy'));
?>

以上例程会输出:

string(5) "dummy"

uopz_del_function

Deletes previously added function or method

说明

bool uopz_del_function ( string $function )

bool uopz_del_function ( string $class , string $function [, int &$all = TRUE ] )

Deletes a previously added function or method.

参数

class
The name of the class.

function
The name of the function or method.

all
Whether all classes that descend from class will also be affected.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

错误/异常

uopz_del_function throws a RuntimeException if the function or method to delete has not been added by uopz_add_function.

范例

示例 #1 Basic uopz_del_function Usage

<?php
uopz_add_function('foo', function () {echo 'bar';});
var_dump(function_exists('foo'));
uopz_del_function('foo');
var_dump(function_exists('foo'));
?>

以上例程会输出:

bool(true)
bool(false)

参见

  • uopz_add_function
  • uopz_unset_return

uopz_delete

Delete a function

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_delete ( string $function )

void uopz_delete ( string $class , string $function )

Deletes a function or method

参数

class

function

返回值

范例

示例 #1 uopz_delete example

<?php
uopz_delete("strlen");

echo strlen("Hello World");
?>

以上例程的输出类似于:

PHP Fatal error: Call to undefined function strlen() in /path/to/script.php on line 4

示例 #2 uopz_delete class example

<?php
class My {
    public static function strlen($arg) {
        return strlen($arg);
    }
}

uopz_delete(My::class, "strlen");

echo My::strlen("Hello World");
?>

以上例程的输出类似于:

PHP Fatal error: Call to undefined method My::strlen() in /path/to/script.php on line 10

uopz_extend

Extend a class at runtime

说明

bool uopz_extend ( string $class , string $parent )

Makes class extend parent

参数

class
The name of the class to extend

parent
The name of the class to inherit

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 uopz_extend example

<?php
class A {}
class B {}

uopz_extend(A::class, B::class);

var_dump(class_parents(A::class));
?>

以上例程会输出:

array(1) {
  ["B"]=>
  string(1) "B"
}

uopz_flags

Get or set flags on function or class

说明

int uopz_flags ( string $function [, int $flags = PHP_INT_MAX ] )

int uopz_flags ( string $class , string $function [, int $flags = PHP_INT_MAX ] )

Get or set the flags on a class or function entry at runtime

参数

class
The name of a class

function
The name of the function

flags
A valid set of ZEND_ACC_ flags. If omitted, uopz_flags acts as getter.

返回值

If setting, returns old flags, else returns flags

更新日志

版本说明
PECL uopz 5.0.0The flags parameter is now optional. Formerly, ZEND_ACC_FETCH had to be passed to use uopz_flags as getter.

范例

示例 #1 uopz_flags example

<?php
class Test {
    public function method() {
        return __CLASS__;
    }
}

$flags = uopz_flags("Test", "method");

var_dump((bool) (uopz_flags("Test", "method") & ZEND_ACC_PRIVATE));
var_dump((bool) (uopz_flags("Test", "method") & ZEND_ACC_STATIC));

var_dump(uopz_flags("Test", "method", $flags|ZEND_ACC_STATIC|ZEND_ACC_PRIVATE));

var_dump((bool) (uopz_flags("Test", "method") & ZEND_ACC_PRIVATE));
var_dump((bool) (uopz_flags("Test", "method") & ZEND_ACC_STATIC));
?>

以上例程会输出:

bool(false)
bool(false)
int(1234567890)
bool(true)
bool(true)

uopz_function

Creates a function at runtime

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_function ( string $function , Closure $handler [, int $modifiers ] )

void uopz_function ( string $class , string $function , Closure $handler [, int $modifiers ] )

Creates a function at runtime

参数

class
The name of the class to receive the new function

function
The name of the function

handler
The Closure for the function

modifiers
The modifiers for the function, by default copied or ZEND_ACC_PUBLIC

返回值

范例

示例 #1 uopz_function example

<?php
uopz_function("my_strlen", function($arg) {
    return strlen($arg);
});
echo my_strlen("Hello World");
?>

以上例程会输出:

11

示例 #2 uopz_function class example

<?php
class My {}

uopz_function(My::class, "strlen", function($arg) {
    return strlen($arg);
}, ZEND_ACC_STATIC);

echo My::strlen("Hello World");
?>

以上例程会输出:

11

uopz_get_exit_status

Retrieve the last set exit status

说明

mixed uopz_get_exit_status ( void )

Retrieves the last set exit status, i.e. the value passed to exit.

参数

此函数没有参数。

返回值

This function returns the last exit status, or NULL if exit has not been called.

范例

示例 #1 uopz_get_exit_status example

<?php
exit(123); 
echo uopz_get_exit_status();?>

以上例程会输出:

123

注释

Caution

OPcache optimizes away dead code after unconditional exit.

参见

  • uopz_allow_exit

uopz_get_hook

Gets previously set hook on function or method

说明

Closure uopz_get_hook ( string $function )

Closure uopz_get_hook ( string $class , string $function )

Gets the previously set hook on a function or method.

参数

class
The name of the class.

function
The name of the function or method.

返回值

Returns the previously set hook on a function or method, or NULL if no hook has been set.

范例

示例 #1 Basic uopz_get_hook Usage

<?php
function foo() {
    echo 'foo';
}
uopz_set_hook('foo', function () {echo 'bar';});
var_dump(uopz_get_hook('foo'));
?>

以上例程的输出类似于:

object(Closure)#2 (0) {
}

参见

  • uopz_set_hook
  • uopz_unset_hook

uopz_get_mock

Get the current mock for a class

说明

mixed uopz_get_mock ( string $class )

Returns the current mock for class.

参数

class
The name of the mocked class.

返回值

Either a string containing the name of the mock, or an object, or NULL if no mock has been set.

范例

示例 #1 uopz_get_mock example

<?php
class A {
    public static function who() {
        echo "A";
    }
}

class mockA {
    public static function who() {
        echo "mockA";
    }
}

uopz_set_mock(A::class, mockA::class);
echo uopz_get_mock(A::class);
?>

以上例程会输出:

mockA

参见

  • uopz_set_mock
  • uopz_unset_mock

uopz_get_property

Gets value of class or instance property

说明

mixed uopz_get_property ( string $class , string $property )

mixed uopz_get_property ( object $instance , string $property )

Gets the value of a static class property, if class is given, or the value of an instance property, if instance is given.

参数

class
The name of the class.

instance
The object instance.

property
The name of the property.

返回值

Returns the value of the class or instance property, or NULL if the property is not defined.

范例

示例 #1 Basic uopz_get_property Usage

<?php
class Foo {
    private static $staticBar = 10;
    private $bar = 100;
}
$foo = new Foo;
var_dump(uopz_get_property('Foo', 'staticBar'));
var_dump(uopz_get_property($foo, 'bar'));
?>

以上例程的输出类似于:

int(10)
int(100)

参见

  • uopz_set_property

uopz_get_return

Gets a previous set return value for a function

说明

mixed uopz_get_return ( string $function )

mixed uopz_get_return ( string $class , string $function )

Gets the return value of the function previously set by uopz_set_return.

参数

class
The name of the class containing the function

function
The name of the function

返回值

The return value or Closure previously set.

范例

示例 #1 uopz_get_return example

<?php
uopz_set_return("strlen", 42);
echo uopz_get_return("strlen");
?>

以上例程会输出:

42

uopz_get_static

Gets the static variables from function or method scope

说明

array uopz_get_static ( string $class , string $function )

array uopz_get_static ( string $function )

Gets the static variables from function or method scope.

参数

class
The name of the class.

function
The name of the function or method.

返回值

Returns an associative array of variable names mapped to their current values on success, or NULL if the function or method does not exist.

范例

示例 #1 Basic uopz_get_static Usage

<?php
function foo() {
    static $bar = 'baz';
}
var_dump(uopz_get_static('foo'));
?>

以上例程会输出:

array(1) {
  ["bar"]=>
  string(3) "baz"
}

参见

  • uopz_set_static

uopz_implement

Implements an interface at runtime

说明

bool uopz_implement ( string $class , string $interface )

Makes class implement interface

参数

class

interface

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 uopz_implement example

<?php
interface myInterface {}

class myClass {}

uopz_implement(myClass::class, myInterface::class);

var_dump(class_implements(myClass::class));
?>

以上例程会输出:

array(1) {
  ["myInterface"]=>
  string(11) "myInterface"
}

uopz_overload

Overload a VM opcode

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_overload ( int $opcode , Callable $callable )

Overloads the specified opcode with the user defined function

参数

opcode
A valid opcode, see constants for details of supported codes

callable

返回值

范例

示例 #1 uopz_overload example

<?php
uopz_overload(ZEND_EXIT, function(){});

exit();
echo "Hello World";
?>

以上例程会输出:

Hello World

uopz_redefine

Redefine a constant

说明

bool uopz_redefine ( string $constant , mixed $value )

bool uopz_redefine ( string $class , string $constant , mixed $value )

Redefines the given constant as value

参数

class
The name of the class containing the constant

constant
The name of the constant

value
The new value for the constant, must be a valid type for a constant variable

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 uopz_redefine example

<?php
define("MY", 100);

uopz_redefine("MY", 1000);

echo MY;
?>

以上例程会输出:

1000

uopz_rename

Rename a function at runtime

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_rename ( string $function , string $rename )

void uopz_rename ( string $class , string $function , string $rename )

Renames function to rename

Note:

If both functions exist, this effectively swaps their names

参数

class
The name of the class containing the function

function
The name of an existing function

rename
The new name for the function

返回值

范例

示例 #1 uopz_rename example

<?php
uopz_rename("strlen", "original_strlen");

echo original_strlen("Hello World");
?>

以上例程会输出:

11

示例 #2 uopz_rename class example

<?php
class My {
    public function strlen($arg) {
        return strlen($arg);
    }
}

uopz_rename(My::class, "strlen", "original_strlen");

echo My::original_strlen("Hello World");
?>

以上例程会输出:

11

uopz_restore

Restore a previously backed up function

Warning

This function has been REMOVED in PECL uopz 5.0.0.

说明

void uopz_restore ( string $function )

void uopz_restore ( string $class , string $function )

Restore a previously backed up function

参数

class
The name of the class containing the function to restore

function
The name of the function

返回值

范例

示例 #1 uopz_restore example

<?php
uopz_backup("fgets");
uopz_function("fgets", function(){
    return true;
});
var_dump(fgets());
uopz_restore('fgets');
fgets();
?>

以上例程的输出类似于:

Warning: fgets() expects at least 1 parameter, 0 given in /path/to/script.php on line 8

uopz_set_hook

Sets hook to execute when entering a function or method

说明

bool uopz_set_hook ( string $function , Closure $hook )

bool uopz_set_hook ( string $class , string $function , Closure $hook )

Sets a hook to execute when entering a function or method.

参数

class
The name of the class.

function
The name of the function or method.

hook
A closure to execute when entering the function or method.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Basic uopz_set_hook Usage

<?php
function foo() {
    echo 'foo';
}
uopz_set_hook('foo', function () {echo 'bar';});
foo();
?>

以上例程会输出:

barfoo

参见

  • uopz_get_hook
  • uopz_unset_hook

uopz_set_mock

Use mock instead of class for new objects

说明

void uopz_set_mock ( string $class , mixed $mock )

If mock is a string containing the name of a class then it will be instantiated instead of class. mock can also be an object.

Note:

Only dynamic access to properties and methods will use the mock object. Static access still uses the original class. See example below.

参数

class
The name of the class to be mocked.

mock
The mock to use in the form of a string containing the name of the class to use or an object. If a string is passed, it has to be the fully qualified class name. It is recommended to use the ::class magic constant in this case.

更新日志

版本说明
uopz 6.0.0Mocking static members is no longer supported by this function. uopz_redefine and uopz_set_return, or Componere can be used instead.

范例

示例 #1 uopz_set_mock example

<?php
class A {
    public function who() {
        echo "A";
    }
}

class mockA {
    public function who() {
        echo "mockA";
    }
}

uopz_set_mock(A::class, mockA::class);
(new A)->who();
?>

以上例程会输出:

mockA

示例 #2 uopz_set_mock example

<?php
class A {
    public function who() {
        echo "A";
    }
}

uopz_set_mock(A::class, new class {
                            public function who() {
                                echo "mockA";
                            }
                        });
(new A)->who();
?>

以上例程会输出:

mockA

示例 #3 uopz_set_mock and static members

As of uopz 6.0.0 mocking static members is no longer supported.

<?php
class A {
    const CON = 'A';
    public static function who() {
        echo "A";
    }
}

uopz_set_mock(A::class, new class {
                            const CON = 'mockA';
                            public static function who() {
                                echo "mockA";
                            }
                        });
echo A::CON, PHP_EOL;
A::who();
?>

以上例程会输出:

A
A

Output of the above example in uopz 5:

mockA
mockA

参见

  • uopz_get_mock
  • uopz_unset_mock

uopz_set_property

Sets value of existing class or instance property

说明

void uopz_set_property ( string $class , string $property , mixed $value )

void uopz_set_property ( object $instance , string $property , mixed $value )

Sets the value of an existing static class property, if class is given, or the value of an instance property (regardless whether the instance property already exists), if instance is given.

参数

class
The name of the class.

instance
The object instance.

property
The name of the property.

value
The value to assign to the property.

返回值

没有返回值。

范例

示例 #1 Basic uopz_set_property Usage

<?php
class Foo {
   private static $staticBar;
   private $bar;
   public static function testStaticBar() {
      return self::$staticBar;
   }
   public function testBar() {
      return $this->bar;
   }
}
$foo = new Foo;
uopz_set_property('Foo', 'staticBar', 10);
uopz_set_property($foo, 'bar', 100);
var_dump(Foo::testStaticBar());
var_dump($foo->testBar());
?>

以上例程会输出:

int(10)

参见

  • uopz_get_property

uopz_set_return

Provide a return value for an existing function

说明

bool uopz_set_return ( string $function , mixed $value [, bool $execute = FALSE ] )

bool uopz_set_return ( string $class , string $function , mixed $value [, bool $execute = FALSE ] )

Sets the return value of the function to value. If value is a Closure and execute is set, the Closure will be executed in place of the original function. It is possible to call the original function from the Closure.

Note:

This function replaces uopz_rename.

参数

class
The name of the class containing the function

function
The name of an existing function

value
The value the function should return. If a Closure is provided and the execute flag is set, the Closure will be executed in place of the original function.

execute
If true, and a Closure was provided as the value, the Closure will be executed in place of the original function.

返回值

True if succeeded, false otherwise.

范例

示例 #1 uopz_set_return example

<?php
uopz_set_return("strlen", 42);
echo strlen("Banana");
?>

以上例程会输出:

42

示例 #2 uopz_set_return example

<?php
uopz_set_return("strlen", function($str) { return strlen($str) * 2; }, true );
echo strlen("Banana");
?>

以上例程会输出:

12

示例 #3 uopz_set_return class example

<?php
class My {
    public static function strlen($arg) {
        return strlen($arg);
    }
}
uopz_set_return(My::class, "strlen", function($str) { return strlen($str) * 2; }, true );
echo My::strlen("Banana");
?>

以上例程会输出:

12

uopz_set_static

Sets the static variables in function or method scope

说明

void uopz_set_static ( string $function , array $static )

void uopz_set_static ( string $class , string $function , array $static )

Sets the static variables in function or method scope.

参数

class
The name of the class.

function
The name of the function or method.

static
The associative array of variable names mapped to their values.

返回值

没有返回值。

范例

示例 #1 Basic uopz_set_static Usage

<?php
function foo() {
    static $bar = 'baz';
    var_dump($bar);
}
uopz_set_static('foo', ['bar' => 'qux']);
foo();
?>

以上例程会输出:

string(3) "qux"

参见

  • uopz_get_static

uopz_undefine

Undefine a constant

说明

bool uopz_undefine ( string $constant )

bool uopz_undefine ( string $class , string $constant )

Removes the constant at runtime

参数

class
The name of the class containing constant

constant
The name of an existing constant

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 uopz_undefine example

<?php
define("MY", true);

uopz_undefine("MY");

var_dump(defined("MY"));
?>

以上例程会输出:

bool(false)

uopz_unset_hook

Removes previously set hook on function or method

说明

bool uopz_unset_hook ( string $function )

bool uopz_unset_hook ( string $class , string $function )

Removes the previously set hook on a function or method.

参数

class
The name of the class.

function
The name of the function or method.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Basic uopz_unset_hook Usage

<?php
function foo() {
    echo 'foo';
}
uopz_set_hook('foo', function () {echo 'bar';});
foo();
echo PHP_EOL;
uopz_unset_hook('foo');
foo();
?>

以上例程会输出:

barfoo
foo

参见

  • uopz_set_hook
  • uopz_get_hook

uopz_unset_mock

Unset previously set mock

说明

void uopz_unset_mock ( string $class )

Unsets the previously set mock for class.

参数

class
The name of the mocked class.

错误/异常

A RuntimeException is thrown, if no mock was previously set for class.

范例

示例 #1 uopz_unset_mock example

<?php
class A {
    public static function who() {
        echo "A";
    }
}

class mockA {
    public static function who() {
        echo "mockA";
    }
}

uopz_set_mock(A::class, mockA::class);
uopz_unset_mock(A::class);
A::who();
?>

以上例程会输出:

A

参见

  • uopz_set_mock
  • uopz_get_mock

uopz_unset_return

Unsets a previously set return value for a function

说明

bool uopz_unset_return ( string $function )

bool uopz_unset_return ( string $class , string $function )

Unsets the return value of the function previously set by uopz_set_return.

参数

class
The name of the class containing the function

function
The name of the function

返回值

True on success

范例

示例 #1 uopz_unset_return example

<?php
uopz_set_return("strlen", 42);
$len = strlen("Banana");
uopz_unset_return("strlen");
echo $len + strlen("Banana");
?>

以上例程会输出:

48

目录