码迷,mamicode.com
首页 > Web开发 > 详细

php 生成类的对象

时间:2015-01-11 22:56:48      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:

 

static int ZEND_FASTCALL  ZEND_NEW_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{
    USE_OPLINE
    zval *object_zval;
    zend_function *constructor;

    SAVE_OPLINE();
    if (UNEXPECTED((EX_T(opline->op1.var).class_entry->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) != 0)) {
        if (EX_T(opline->op1.var).class_entry->ce_flags & ZEND_ACC_INTERFACE) {
            zend_error_noreturn(E_ERROR, "Cannot instantiate interface %s", EX_T(opline->op1.var).class_entry->name);
        } else if ((EX_T(opline->op1.var).class_entry->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT) {
            zend_error_noreturn(E_ERROR, "Cannot instantiate trait %s", EX_T(opline->op1.var).class_entry->name);
        } else {
            zend_error_noreturn(E_ERROR, "Cannot instantiate abstract class %s", EX_T(opline->op1.var).class_entry->name);
        }
    }
    ALLOC_ZVAL(object_zval);
    object_init_ex(object_zval, EX_T(opline->op1.var).class_entry);
    INIT_PZVAL(object_zval);

    constructor = Z_OBJ_HT_P(object_zval)->get_constructor(object_zval TSRMLS_CC);

    if (constructor == NULL) {
        if (RETURN_VALUE_USED(opline)) {
            AI_SET_PTR(&EX_T(opline->result.var), object_zval);
        } else {
            zval_ptr_dtor(&object_zval);
        }
        ZEND_VM_JMP(EX(op_array)->opcodes + opline->op2.opline_num);
    } else {
        if (RETURN_VALUE_USED(opline)) {
            PZVAL_LOCK(object_zval);
            AI_SET_PTR(&EX_T(opline->result.var), object_zval);
        }

        zend_ptr_stack_3_push(&EG(arg_types_stack), EX(fbc), EX(object), ENCODE_CTOR(EX(called_scope), RETURN_VALUE_USED(opline)));

        /* We are not handling overloaded classes right now */
        EX(object) = object_zval;
        EX(fbc) = constructor;
        EX(called_scope) = EX_T(opline->op1.var).class_entry;

        CHECK_EXCEPTION();
        ZEND_VM_NEXT_OPCODE();
    }
}

 

 

#define object_init_ex(arg, ce)    _object_init_ex((arg), (ce) ZEND_FILE_LINE_CC TSRMLS_CC)

ZEND_API int _object_init_ex(zval *arg, zend_class_entry *class_type ZEND_FILE_LINE_DC TSRMLS_DC) /* {{{ */
{
    return _object_and_properties_init(arg, class_type, 0 ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
}

ZEND_API int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties ZEND_FILE_LINE_DC TSRMLS_DC) /* {{{ */
{
    zend_object *object;

    if (class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
        char *what =   (class_type->ce_flags & ZEND_ACC_INTERFACE)                ? "interface"
                     :((class_type->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT) ? "trait"
                     :                                                              "abstract class";
        zend_error(E_ERROR, "Cannot instantiate %s %s", what, class_type->name);
    }

    zend_update_class_constants(class_type TSRMLS_CC);

    Z_TYPE_P(arg) = IS_OBJECT;
    if (class_type->create_object == NULL) {
        Z_OBJVAL_P(arg) = zend_objects_new(&object, class_type TSRMLS_CC);
        if (properties) {
            object->properties = properties;
            object->properties_table = NULL;
        } else {
            object_properties_init(object, class_type);
        }
    } else {
        Z_OBJVAL_P(arg) = class_type->create_object(class_type TSRMLS_CC);
    }
    return SUCCESS;
}

 

 

ZEND_API zend_object_value zend_objects_new(zend_object **object, zend_class_entry *class_type TSRMLS_DC)
{
    zend_object_value retval;

    *object = emalloc(sizeof(zend_object));
    (*object)->ce = class_type;
    (*object)->properties = NULL;
    (*object)->properties_table = NULL;
    (*object)->guards = NULL;
    retval.handle = zend_objects_store_put(*object, (zend_objects_store_dtor_t) zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_objects_free_object_storage, NULL TSRMLS_CC);
    retval.handlers = &std_object_handlers;
    return retval;
}

ZEND_API zend_object_handle zend_objects_store_put(void *object, zend_objects_store_dtor_t dtor, zend_objects_free_object_storage_t free_storage, zend_objects_store_clone_t clone TSRMLS_DC)
{
    zend_object_handle handle;
    struct _store_object *obj;

    if (EG(objects_store).free_list_head != -1) {
        handle = EG(objects_store).free_list_head;
        EG(objects_store).free_list_head = EG(objects_store).object_buckets[handle].bucket.free_list.next;
    } else {
        if (EG(objects_store).top == EG(objects_store).size) {
            EG(objects_store).size <<= 1;
            EG(objects_store).object_buckets = (zend_object_store_bucket *) erealloc(EG(objects_store).object_buckets, EG(objects_store).size * sizeof(zend_object_store_bucket));
        }
        handle = EG(objects_store).top++;
    }
    obj = &EG(objects_store).object_buckets[handle].bucket.obj;
    EG(objects_store).object_buckets[handle].destructor_called = 0;
    EG(objects_store).object_buckets[handle].valid = 1;
    EG(objects_store).object_buckets[handle].apply_count = 0;

    obj->refcount = 1;
    GC_OBJ_INIT(obj);
    obj->object = object;
    obj->dtor = dtor?dtor:(zend_objects_store_dtor_t)zend_objects_destroy_object;
    obj->free_storage = free_storage;
    obj->clone = clone;
    obj->handlers = NULL;

#if ZEND_DEBUG_OBJECTS
    fprintf(stderr, "Allocated object id #%d\n", handle);
#endif
    return handle;
}

 

 

 

 

php 生成类的对象

标签:

原文地址:http://www.cnblogs.com/taek/p/4129187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!