DEVSENSE / Phalanger

PHP 5.4 compiler for .NET/Mono frameworks. Predecessor to the opensource PeachPie project (www.peachpie.io).
http://v4.php-compiler.net/
Apache License 2.0
381 stars 92 forks source link

Help, Custom entity class, can not convert type to [PHP.Core.IPhpVariable] #56

Closed taotao2014 closed 9 years ago

taotao2014 commented 9 years ago

=====c# code:======

static void Main(string[] args) { ScriptContext context = ScriptContext.CurrentContext; context.Output = Console.Out; context.Include(@"C:\script.php", true); PhpReference r = context.Call("myTest", new WebClient(), new Student() { Age = 20, UserName = "David" }); Console.WriteLine(r.Value); Console.ReadLine();

}

 public class Student
    {
        public string UserName { get; set; }
        public int Age { get; set; }
    }

=====script file:=====

<? use System\Net; use CSharpApp;

function myTest($webClient,$student)
{
    System\Diagnostics\Debug::WriteLine($student);
    var_dump($student);
    var_export($student);
    //$student->Age=1000;
            echo $student->Age; //error,why??
    //echo $student->UserName;
            //$page = $webClient->DownloadString('http://www.google.com');
            //echo $page;
    return $student;
}

?>

jakubmisek commented 9 years ago

hi,

There are few limitations when using this old API (context.Call). You have to 'wrap' Student into ClrObject first, so phalanger will be able to use it properly.

Use following call instead, so all this stuff is performed automatically

dynamic php = context.Globals; // dynamic context used to call functions, instantiate classes etc..
Student student = php.myTest(new WebClient(), new Student() { Age = 20, UserName = "David" });

for more info about dynamic context see Globals at http://www.php-compiler.net/blog/2012/net-interoperability-overview-of-phalanger-3-0

taotao2014 commented 9 years ago

New, old model, have been resolved, thank you very much.