Skip to content

Commit c018e5c

Browse files
xingskycnxingskycn
authored andcommitted
add objects3
1 parent 536bcaf commit c018e5c

File tree

9 files changed

+331
-0
lines changed

9 files changed

+331
-0
lines changed

codes/objects3/.svnignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.deps
2+
*.lo
3+
*.la

codes/objects3/CREDITS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
objects3

codes/objects3/EXPERIMENTAL

Whitespace-only changes.

codes/objects3/config.m4

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PHP_ARG_WITH(objects3, for objects3 support,
2+
[ --with-objects3 Include objects3 support])
3+
4+
PHP_ARG_ENABLE(objects3, whether to enable objects3 support,
5+
[ --enable-objects3 Enable objects3 support])
6+
7+
if test "$PHP_OBJECTS3" != "no"; then
8+
PHP_NEW_EXTENSION(objects3, objects3.c, $ext_shared)
9+
fi

codes/objects3/config.w32

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// $Id$
2+
// vim:ft=javascript
3+
4+
// If your extension references something external, use ARG_WITH
5+
// ARG_WITH("objects3", "for objects3 support", "no");
6+
7+
// Otherwise, use ARG_ENABLE
8+
// ARG_ENABLE("objects3", "enable objects3 support", "no");
9+
10+
if (PHP_OBJECTS3 != "no") {
11+
EXTENSION("objects3", "objects3.c");
12+
}
13+

codes/objects3/objects3.c

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2014 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifdef HAVE_CONFIG_H
22+
#include "config.h"
23+
#endif
24+
25+
#include "php.h"
26+
#include "php_ini.h"
27+
#include "ext/standard/info.h"
28+
#include "php_objects3.h"
29+
30+
31+
static zend_class_entry *hello_ce;
32+
static zend_object_handlers hello_handlers;
33+
34+
35+
typedef struct _hello_object {
36+
zend_object obj;
37+
38+
char *name;
39+
int name_len;
40+
long age;
41+
} hello_object;
42+
43+
#define HELLO_FETCH_OBJECT(zobj) (hello_object *)zend_object_store_get_object((zobj) TSRMLS_CC)
44+
45+
ZEND_BEGIN_ARG_INFO(hello___construct_arginfo, 0)
46+
ZEND_ARG_INFO(0, name)
47+
ZEND_ARG_INFO(0, age)
48+
ZEND_END_ARG_INFO()
49+
static PHP_METHOD(Hello, __construct) {
50+
hello_object *objval = HELLO_FETCH_OBJECT(getThis());
51+
char *name;
52+
int name_len;
53+
long age;
54+
55+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &age) == FAILURE) {
56+
return;
57+
}
58+
59+
objval->name = estrndup(name, name_len);
60+
objval->name_len = name_len;
61+
objval->age = age;
62+
}
63+
64+
65+
static PHP_METHOD(Hello, greet) {
66+
hello_object *objval = HELLO_FETCH_OBJECT(getThis());
67+
68+
php_printf("Hello, ");
69+
PHPWRITE(objval->name, objval->name_len);
70+
php_printf(", you appear to be %ld years old\n", objval->age);
71+
}
72+
73+
static zend_function_entry hello_methods[] = {
74+
PHP_ME(Hello, __construct, hello___construct_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
75+
PHP_ME(Hello, greet, NULL, ZEND_ACC_PUBLIC)
76+
{NULL, NULL, NULL}
77+
};
78+
79+
static void hello_dtor(hello_object *objval TSRMLS_CC) {
80+
if (objval->name) {
81+
efree(objval->name);
82+
}
83+
zend_object_std_dtor(&(objval->obj) TSRMLS_CC);
84+
85+
efree(objval);
86+
}
87+
88+
static zend_object_value hello_ctor(zend_class_entry *ce TSRMLS_DC) {
89+
hello_object *objval = emalloc(sizeof(hello_object));
90+
memset(objval, 0, sizeof(hello_object));
91+
92+
zend_object_value retval;
93+
zend_object_std_init(&(objval->obj), ce TSRMLS_CC);
94+
95+
retval.handle = zend_objects_store_put(objval ,
96+
NULL,
97+
(zend_objects_free_object_storage_t)hello_dtor,
98+
NULL TSRMLS_CC);
99+
retval.handlers = &hello_handlers;
100+
101+
return retval;
102+
}
103+
104+
105+
static zend_object_value hello_ctor_ex(hello_object **pobjval, zend_class_entry *ce TSRMLS_DC) {
106+
hello_object *objval = emalloc(sizeof(hello_object));
107+
memset(objval, 0, sizeof(hello_object));
108+
109+
zend_object_value retval;
110+
zend_object_std_init(&(objval->obj), ce TSRMLS_CC);
111+
112+
retval.handle = zend_objects_store_put(objval,
113+
NULL,
114+
(zend_objects_free_object_storage_t) hello_dtor,
115+
NULL TSRMLS_CC);
116+
retval.handlers = &hello_handlers;
117+
118+
if (pobjval) {
119+
*pobjval = objval;
120+
}
121+
122+
return retval;
123+
}
124+
125+
static zend_object_value hello_clone(zval *zobject TSRMLS_DC) {
126+
hello_object *old_object = HELLO_FETCH_OBJECT(zobject);
127+
hello_object *new_object;
128+
129+
zend_object_value retval = hello_ctor_ex(&new_object, old_object->obj.ce TSRMLS_CC);
130+
131+
zend_objects_clone_members(&(new_object->obj), retval, &(old_object->obj), Z_OBJ_HANDLE_P(zobject) TSRMLS_CC);
132+
133+
if (old_object->name) {
134+
new_object->name = estrndup(old_object->name, old_object->name_len);
135+
new_object->name_len = old_object->name_len;
136+
}
137+
new_object->age = old_object->age;
138+
139+
return retval;
140+
}
141+
142+
143+
static zval *hello_get(zval *zobject TSRMLS_CC) {
144+
//hello_object *ho = HELLO_FETCH_OBJECT(zobject);
145+
zval *val;
146+
MAKE_STD_ZVAL(val);
147+
ZVAL_STRING(val, "bar", 1);
148+
149+
return val;
150+
}
151+
152+
static int hello_count_elements(zval *object, long *count TSRMLS_DC) {
153+
zend_object_handlers *zh = zend_get_std_object_handlers();
154+
php_printf("count: %ld\n", *count);
155+
*count = 5;
156+
return SUCCESS;
157+
}
158+
159+
PHP_MINIT_FUNCTION(objects3){
160+
zend_class_entry ce;
161+
INIT_CLASS_ENTRY(ce, "Hello", hello_methods)
162+
163+
hello_ce = zend_register_internal_class(&ce TSRMLS_CC);
164+
hello_ce->create_object = hello_ctor;
165+
166+
memcpy(&hello_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
167+
hello_handlers.clone_obj = hello_clone;
168+
//hello_handlers.get = hello_get;
169+
hello_handlers.count_elements = hello_count_elements;
170+
return SUCCESS;
171+
}
172+
173+
174+
zend_module_entry objects3_module_entry = {
175+
STANDARD_MODULE_HEADER,
176+
"objects3", /* extname */
177+
NULL, /* functions */
178+
PHP_MINIT(objects3),
179+
NULL, /* MSHUTDOWN */
180+
NULL, /* RINIT */
181+
NULL, /* RSHUTDOWN */
182+
NULL, /* MINFO */
183+
NO_VERSION_YET,
184+
STANDARD_MODULE_PROPERTIES
185+
};
186+
187+
#ifdef COMPILE_DL_OBJECTS3
188+
ZEND_GET_MODULE(objects3)
189+
#endif
190+
191+
192+
/*
193+
* Local variables:
194+
* tab-width: 4
195+
* c-basic-offset: 4
196+
* End:
197+
* vim600: noet sw=4 ts=4 fdm=marker
198+
* vim<600: noet sw=4 ts=4
199+
*/

codes/objects3/objects3.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
$br = (php_sapi_name() == "cli")? "":"<br>";
3+
4+
if(!extension_loaded('objects3')) {
5+
dl('objects3.' . PHP_SHLIB_SUFFIX);
6+
}
7+
8+
9+
$obj = new Hello("leon", 32);
10+
//var_dump($obj);
11+
12+
echo "obj: ";
13+
$obj->greet();
14+
//echo $obj."_abc";
15+
//echo "obj count:" . count($obj)."\n";
16+
17+
18+
$obj1 = clone $obj;
19+
echo "obj1: ";
20+
$obj1->greet();
21+
22+
23+
//var_dump($obj1);
24+
25+
?>

codes/objects3/php_objects3.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2014 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef PHP_OBJECTS3_H
22+
#define PHP_OBJECTS3_H
23+
24+
extern zend_module_entry objects3_module_entry;
25+
#define phpext_objects3_ptr &objects3_module_entry
26+
27+
#define PHP_OBJECTS3_VERSION "0.1.0" /* Replace with version number for your extension */
28+
29+
#ifdef PHP_WIN32
30+
# define PHP_OBJECTS3_API __declspec(dllexport)
31+
#elif defined(__GNUC__) && __GNUC__ >= 4
32+
# define PHP_OBJECTS3_API __attribute__ ((visibility("default")))
33+
#else
34+
# define PHP_OBJECTS3_API
35+
#endif
36+
37+
#ifdef ZTS
38+
#include "TSRM.h"
39+
#endif
40+
41+
PHP_MINIT_FUNCTION(objects3);
42+
43+
44+
#ifdef ZTS
45+
#define OBJECTS3_G(v) TSRMG(objects3_globals_id, zend_objects3_globals *, v)
46+
#else
47+
#define OBJECTS3_G(v) (objects3_globals.v)
48+
#endif
49+
50+
#endif /* PHP_OBJECTS3_H */
51+
52+
53+
/*
54+
* Local variables:
55+
* tab-width: 4
56+
* c-basic-offset: 4
57+
* End:
58+
* vim600: noet sw=4 ts=4 fdm=marker
59+
* vim<600: noet sw=4 ts=4
60+
*/

codes/objects3/tests/001.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Check for objects3 presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("objects3")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
echo "objects3 extension is available";
8+
/*
9+
you can add regression tests for your extension here
10+
11+
the output of your test code has to be equal to the
12+
text in the --EXPECT-- section below for the tests
13+
to pass, differences between the output and the
14+
expected text are interpreted as failure
15+
16+
see php5/README.TESTING for further information on
17+
writing regression tests
18+
*/
19+
?>
20+
--EXPECT--
21+
objects3 extension is available

0 commit comments

Comments
 (0)