getting response string/object from php classes

Discussion about the open-source Phalanger [?] project.

Re: getting response string/object from php classes

Postby owengerig » October 30th, 2012, 1:27 am

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.
owengerig
 
Posts: 10
Joined: October 16th, 2012, 2:50 pm

Re: getting response string/object from php classes

Postby Jakub Misek » October 30th, 2012, 1:49 pm

'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.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: getting response string/object from php classes

Postby owengerig » October 30th, 2012, 2:06 pm

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.
owengerig
 
Posts: 10
Joined: October 16th, 2012, 2:50 pm

Re: getting response string/object from php classes

Postby Jakub Misek » October 30th, 2012, 2:49 pm

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
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Previous

Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 18 guests

cron

User Control Panel

Login

Who is online

In total there are 18 users online :: 0 registered, 0 hidden and 18 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 18 guests