Page 2 of 2

Re: getting response string/object from php classes

PostPosted: October 30th, 2012, 1:27 am
by owengerig
I see 'hi' and 'hiStr' are both null?

They were till I added $hi to the global variables. After that both properties $hi and phpObj.hi showed up with the correct strings as their values. However I still receive the same error message as above.

I can show my code but really most of the functionality Im testing is only using the sample (hence all the hi() functions). The main difference however is the
Code: Select all
Using
code I talked about earlier. I still dont understand why I would need that if its not in the sample. Is there some kind of project settings Im missing?

As you pointed out earlier, these statement do work if placed with the same using block as the declaration of phpObj. But outside of the statement it does not.

Re: getting response string/object from php classes

PostPosted: October 30th, 2012, 1:49 pm
by Jakub Misek
'using' is C# construct that ensures, the object is Disposed at the end of block.

It is recommended to dispose 'RequestContext' because it simulates PHP lifecycle - at the end, PHP objects with __destruct method are finalized. It also cleans some resources. As stated before, all the PHP operations should be performed inside the RequestContext.

Anyway using phpObj outside RequestContext should not cause Null reference exception.

Re: getting response string/object from php classes

PostPosted: October 30th, 2012, 2:06 pm
by owengerig
I noticed the output variable was null as well and figured I would try adding another using statement to the Page_Load function (on Post Backs) and now it works. Kinda of have a feeling this isnt the right way however

Code: Select all
       protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                this.phpObj = (dynamic)HttpContext.Current.Session["phpObj"];
               
                using (var request_context = RequestContext.Initialize(ApplicationContext.Default, HttpContext.Current))
                {
                    //part 1
                    //Gets PHP output stream
                    output = ScriptContext.CurrentContext.Output;
                    //Gets GlobalScope object
                    global = ScriptContext.CurrentContext.Globals;
                }
            }
            else
            {
                using (var request_context = RequestContext.Initialize(ApplicationContext.Default, HttpContext.Current))
                {
                    context = request_context.ScriptContext;
                    context.Include("Client.php", false);
                    context.Include("Crypt.php", false);
                    context.Include("Exception.php", false);

                    //part 1
                    //Gets PHP output stream
                    output = ScriptContext.CurrentContext.Output;
                    //Gets GlobalScope object
                    global = ScriptContext.CurrentContext.Globals;


                    phpObj = global.@class.Fuze_Client("https://partnerdev.fuzemeeting.com/", "83666136", "Q5iI4bcoADj9QGrMC6Ss5V34VK1FxKNZ");

                    //phpObj.hi();
                    //output.WriteLine("hi: {0}<br/>", phpObj.hi);
                    //output.WriteLine("{0}<br/>", phpObj.hi());
                }
                HttpContext.Current.Session["phpObj"] = this.phpObj;
            }

        }


I understand what your saying about simulating the php life cycle but I need these objects to stay initialized for the session.

Re: getting response string/object from php classes

PostPosted: October 30th, 2012, 2:49 pm
by Jakub Misek
The phpObj can stay in session, RequestContext is needed internally for web-oriented functionality in Phalanger, by initializing it at the beginning of request and Disposing it at the end, you will increase performance mostly, and allow Phalanger to cleanup resources etc and objects with __destruct.

You can do it in global.asax:
- at Application_BeginRequest: RequestContext.Initialize( ..., HttpContext.Current )
- at Application_EndRequest: RequestContext.CurrentContext.Dispose();

To get ScriptContext anywhere in the code:
- ScriptContext.Current

Thanks