Can't access PHP builtins from inside subclass constructor

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

Can't access PHP builtins from inside subclass constructor

Postby dancecile » September 6th, 2012, 8:20 pm

I'm trying to run MediaWiki language conversions using Phalanger 3.0.0.3406, but I'm hitting an odd problem with accessing builtin PHP functions from certain places. Even though I've referenced PhpNetClassLibrary, this is the error: "Call to undefined function: 'array_merge'"

I've reduced the problem to a few lines of code across 2 files. The problem goes away when only one file is used. The problem also goes away if I remove the class inheritance.

Here's the first file (example.php):
Code: Select all
<?php

class Language {
}

include "LanguageZh.php";

echo "Top level sleep...\n";
print_r(array_merge(array(1 => 2)));
echo "...done\n";

new LanguageZh();

?>


Here's the second file (LanguageZh.php):
Code: Select all
<?php

class LanguageZh extends Language {
  function __construct() {
    echo "Constructor sleep...\n";
    print_r(array_merge(array(1 => 2)));
    echo "...done\n";
  }
}

?>


And here's how I tried to compile:

Code: Select all
phpc.exe /out:example /entrypoint:example.php example.php /r:PhpNetClassLibrary.dll


The main difference that I see in the compiled bytecode between the crashing-2-file-version of this example and the no-problem-1-file-version, is that LanguageZh turns into a CLR class in the 1-file-version, but in the 2-file-version, it only exists as an unevaluated string argument to DynamicCode.Eval. (I don't know if this is a significant difference or not.)
dancecile
 
Posts: 3
Joined: September 6th, 2012, 7:52 pm

Re: Can't access PHP builtins from inside subclass construct

Postby Jakub Misek » September 6th, 2012, 8:35 pm

Hi,

I tried your test case, and everything works ok for me. I'm getting following output:
Code: Select all
Top level sleep...
Array
(
    [0] => 2
)
...done
Constructor sleep...
Array
(
    [0] => 2
)
...done


DynamicCode.Eval is feature, the code have to be compiled in runtime, because the base class of LanguageZh is not known in compile time (you would have to add 'include "example.php";' in LanguageZh.php to let compiler know, the base class is really that class Language you expect. (imagine you somewhere include LanguageZh.php without example.php ... with another class Language declared)
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: Can't access PHP builtins from inside subclass construct

Postby dancecile » September 6th, 2012, 9:31 pm

OK, that makes sense why DynamicCode.Eval would be used. And yup, the output you got is the correct output.

I'm not quite sure what to do next. I getting same error on both Windows 7 (.NET 4.5) and Ubuntu (Mono 2.10.8), so I don't think I've misconfigured my system. If it makes a difference, I didn't use the Phalanger installer, I'm just using the binaries in the same folder as my PHP files.

I've downloaded the latest Phalanger code from Github. I built phpc.exe, PhpNetClassLibrary.dll, and PhpNetCore.dll, and used them to compile my two files. Unfortunately, I'm still getting the same error. How can I get more info to debug deeper into this?
dancecile
 
Posts: 3
Joined: September 6th, 2012, 7:52 pm

Re: Can't access PHP builtins from inside subclass construct

Postby Jakub Misek » September 6th, 2012, 9:39 pm

Ok, I can see it.

Since the code in DynamicCode.Eval is compiled in run time, you need to let Phalanger know what libraries you reference in run time too.

Following
Code: Select all
/r:PhpNetClassLibrary.dll
tells compiler the reference, but you have to pass the reference to runtime as well. Add example.exe.config file, see http://wiki.php-compiler.net/Configuration/3.0 and include <configSections> and <classLibrary> nodes.
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: Can't access PHP builtins from inside subclass construct

Postby dancecile » September 6th, 2012, 10:10 pm

:D Perfect, thanks. That did the trick. My small example is fixed, and my full MediaWiki language conversion works fine too now.

For the record, here's the example.exe.config file that I used:

Code: Select all
<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="phpNet" type="PHP.Core.ConfigurationSectionHandler, PhpNetCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=0a8e8c4c76728c71" />
  </configSections>
   
  <phpNet>
    <classLibrary>
      <add assembly="PhpNetClassLibrary, Version=3.0.0.0, Culture=neutral, PublicKeyToken=4af37afe3cde05fb" section="bcl" />
    </classLibrary>
  </phpNet>

</configuration>


I can update that wiki page if you'd like. Is it okay if I reorganize it a bit to separate web and non-web applications?
dancecile
 
Posts: 3
Joined: September 6th, 2012, 7:52 pm

Re: Can't access PHP builtins from inside subclass construct

Postby Jakub Misek » September 6th, 2012, 10:28 pm

dancecile wrote:Perfect, thanks. That did the trick. My small example is fixed, and my full MediaWiki language conversion works fine too now.
Great! Thanks for the config file, I'm sure other people will find it helpful.

dancecile wrote:I can update that wiki page if you'd like.
Please, feel free to update any information you find useful. (I have to create you an account)

dancecile wrote:Is it okay if I reorganize it a bit to separate web and non-web applications?
Actually they are the same, just web applications have config file normally, so this wouldn't happen. But it would probably prevent similar issues with Phalanger, so ok.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 16 guests

cron

User Control Panel

Login

Who is online

In total there are 16 users online :: 0 registered, 0 hidden and 16 guests (based on users active over the past 5 minutes)
Most users ever online was 399 on April 19th, 2024, 11:49 pm

Users browsing this forum: No registered users and 16 guests