Esempio n. 1
0
        /// <summary>
        /// Returns an unitialized instance of the specified type or <see cref="__PHP_Incomplete_Class"/>.
        /// </summary>
        /// <param name="typeName">The type name.</param>
        /// <param name="context">Current <see cref="ScriptContext"/>.</param>
        /// <returns>The newly created instance or <B>null</B> if <paramref name="typeName"/> denotes
        /// a primitive type.</returns>
        /// <remarks>
        /// If the <paramref name="typeName"/> denotes a CLR type, no constructor is executed. If the
        /// <paramref name="typeName"/> denotes a PHP type, no user constructor (e.g. <c>__construct</c>)
        /// is executed.
        /// </remarks>
        public static DObject GetUninitializedInstance(string /*!*/ typeName, ScriptContext /*!*/ context)
        {
            // resolve the specified type
            DTypeDesc type = context.ResolveType(typeName);

            if (type == null || type.IsAbstract)
            {
                PhpCallback callback = context.Config.Variables.DeserializationCallback;
                if (callback != null && !callback.IsInvalid)
                {
                    callback.Invoke(typeName);
                    type = context.ResolveType(typeName);

                    if (type == null || type.IsAbstract)
                    {
                        // unserialize_callback_func failed
                        PhpException.Throw(PhpError.Warning, CoreResources.GetString("unserialize_callback_failed",
                                                                                     ((IPhpConvertible)callback).ToString()));
                    }
                }
            }

            if (type == null || type.IsAbstract)
            {
                // type not found -> create __PHP_Incomplete_Class
                __PHP_Incomplete_Class pic = new __PHP_Incomplete_Class(context, false);
                pic.__PHP_Incomplete_Class_Name.Value = typeName;
                pic.__PHP_Incomplete_Class_Name.IsSet = true;

                return(pic);
            }
            else
            {
                // create the instance
                return(type.New(context) as DObject);
            }
        }
Esempio n. 2
0
        public static object New(DTypeDesc type, DTypeDesc caller, ScriptContext context, NamingContext nameContext)
        {
            // error has been thrown by Convert.ObjectToTypeDesc or MakeGenericTypeInstantiation
            if (type == null)
            {
                context.Stack.RemoveFrame();
                return null;
            }

            // interfaces and abstract classes cannot be instantiated
            if (type.IsAbstract)
            {
                context.Stack.RemoveFrame();
                PhpException.CannotInstantiateType(type.MakeFullName(), type.IsInterface);
                return null;
            }

            return type.New(context.Stack, caller, nameContext);
        }
Esempio n. 3
0
        public static PhpArray GetClassVars(DTypeDesc caller, string className, bool parentsFirst, bool includeStatic)
        {
            ScriptContext script_context = ScriptContext.CurrentContext;

            DTypeDesc type = script_context.ResolveType(className);

            if (type == null)
            {
                return(null);
            }

            // determine the calling type
            //DTypeDesc caller = PhpStackTrace.GetClassContext();
            if (caller != null && caller.IsUnknown)
            {
                caller = PhpStackTrace.GetClassContext();
            }
            PhpArray result = new PhpArray();

            // add instance properties
            bool have_instance_props = false;

            if (!type.IsAbstract)
            {
                // the class has to be instantiated in order to discover default instance property values
                // (the constructor will initialize default properties, user defined constructor will not be called)
                DObject obj = type.New(script_context) as DObject;
                if (obj == null)
                {
                    return(null);
                }

                // populate the resulting array taking into account current caller
                IDictionaryEnumerator enumerator = obj.GetEnumerator(caller);
                while (enumerator.MoveNext())
                {
                    result.Add(enumerator.Key, enumerator.Value);
                }

                have_instance_props = true;
            }

            // add static fields (static and instance fields if the type is abstract)
            if (includeStatic)
            {
                foreach (KeyValuePair <VariableName, DPropertyDesc> pair in type.EnumerateProperties(caller))
                {
                    if (pair.Value.IsStatic)
                    {
                        result.Add(pair.Key.ToString(), pair.Value.Get(null));
                    }
                    else if (!have_instance_props)
                    {
                        result.Add(pair.Key.ToString(), null);
                    }
                }
            }

            result.InplaceCopyOnReturn = true;
            return(result);
        }