calling .net constructor from inheriting php class

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

calling .net constructor from inheriting php class

Postby weirdan » January 3rd, 2016, 8:32 pm

I can't find a way to call .net constructor from inheriting php class:
Code: Select all
❯ cat ../e.php
<?php
use System\Uri;

class ConstructorTest
{
    public static function main()
    {
    }
}

class CustomUri extends Uri
{
    public function __construct($uriString)
    {
        parent::__construct($uriString);
    }
}
❯ Deployment/Debug/phpc.exe ../e.php /lang:CLR /r:/usr/lib/mono/4.5/mscorlib.dll /r:/usr/lib/mono/phalanger/PhpNetClassLibrary.dll /r:/usr/lib/mono/4.5/System.dll /out:e.exe /target:exe /encoding:UTF-8 /pure+ /entrypoint:../e.php /debug+
Phalanger - the PHP Language Compiler - version 4.0
for Microsoft (R) .NET Framework version 4.0
Arguments:
../e.php
/lang:CLR
/r:/usr/lib/mono/4.5/mscorlib.dll
/r:/usr/lib/mono/phalanger/PhpNetClassLibrary.dll
/r:/usr/lib/mono/4.5/System.dll
/out:e.exe
/target:exe
/encoding:UTF-8
/pure+
/entrypoint:../e.php
/debug+

Encoding  cannot be loaded.
fileEncoding=""

Loaded libraries:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Base Library (PhpNetClassLibrary)
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Performing compilation ...
/home/weirdan/src/e.php(13,21): error PHP1240: Expecting parent constructor call
/home/weirdan/src/e.php(15,9): error PHP1066: Class 'System\Uri' does not contain definition of method '__construct'

Statistics:
Compiled in 0:00.235.
AST: LibraryFunctionCalls = 0, UnknownFunctionCalls = 0


Build complete -- 2 errors, 0 warnings.


Things I tried:
  1. parent::__construct($string); // errors shown above
  2. $this->Uri($string); // error PHP1240: Expecting parent constructor call
  3. parent::Uri($string); // error PHP1240: Expecting parent constructor call
  4. $this->{".ctor"}($string); // error PHP1240: Expecting parent constructor call
  5. parent::{".ctor"}($string); // error PHP2014: Syntax error: unexpected token '{'
  6. call_user_func([$this, ".ctor"], $string); // error PHP1240: Expecting parent constructor call
  7. call_user_func(["Uri", ".ctor"], $string); // error PHP1240: Expecting parent constructor call
  8. call_user_func(["Uri", "Uri"], $string); // error PHP1240: Expecting parent constructor call
  9. call_user_func(["Uri", "__construct"], $string); // error PHP1240: Expecting parent constructor call
  10. Uri::Uri($string); // error PHP1240: Expecting parent constructor call

So how does one call the constructor?
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: calling .net constructor from inheriting php class

Postby weirdan » January 3rd, 2016, 8:43 pm

As a workaround I'm using static factory functions instead, but I would rather not:
Code: Select all
class CustomUri {
   protected $_something;
   public static function createWithSomething($uri, $something) {
       $instance = new self($uri);
       $instance->_something = $something;
       return $instance;
   }
   public function getSomething() {
       return $this->_something;
   }
}

// somewhere in the code
$uri = CustomUri::createWithSomething("http://google.com", "sfsdfsdf");
var_dump($uri->getSomething());
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: calling .net constructor from inheriting php class

Postby Jakub Misek » January 4th, 2016, 9:34 pm

Hi,

I have to dig a little into the Phalanger parser ... since some .NET limitations, there is an additional syntax for that.

Code: Select all
function __construct($num) :parent($num) { }


Example:
Code: Select all
class X extends \System\Collections\ArrayList
{
    function __construct($num)
      :parent($num)
   {
      
   }
}


In a future, we might remove this syntax extension.
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: calling .net constructor from inheriting php class

Postby weirdan » January 4th, 2016, 10:59 pm

All right, this is something I can live with. For the sake of clarity, parent constructor call specified in this way is executed before invoking __construct(), so you can't modify parameters before they get to parent.

Thanks.
weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: calling .net constructor from inheriting php class

Postby Jakub Misek » January 4th, 2016, 11:06 pm

weirdan wrote: so you can't modify parameters before they get to parent.
you should be able to pass any expression into parents .ctor, so you can modify them e.g.
Code: Select all
:parent($capacity * 10)
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: calling .net constructor from inheriting php class

Postby weirdan » January 4th, 2016, 11:20 pm

Then it's probably not just any expression, because it doesn't seem to work for me:
Code: Select all
❯ cat ../e.php
<?php
use System\Uri;

class ConstructorTest
{
    public static function main()
    {
        $uri = new CustomUri('http://google.com');
    }
}

class CustomUri extends Uri
{
    public function __construct($uriString) : parent($uriString . "/something")
    {
        var_dump($this);
    }
}

❯ ./e.exe
Encoding  cannot be loaded.
fileEncoding=""

object(CustomUri)#147216259 (21) {
  ["AbsolutePath"]=>
  string(1) "/"
  ["AbsoluteUri"]=>
  string(18) "http://google.com/"
  ["Authority"]=>
  string(10) "google.com"
  ["Fragment"]=>
  string(0) ""
  ["Host"]=>
  string(10) "google.com"
  ["HostNameType"]=>
  int(2)
  ["IsDefaultPort"]=>
  bool(true)
  ["IsFile"]=>
  bool(false)
  ["IsLoopback"]=>
  bool(false)
  ["IsUnc"]=>
  bool(false)
  ["LocalPath"]=>
  string(1) "/"
  ["PathAndQuery"]=>
  string(1) "/"
  ["Port"]=>
  int(80)
  ["Query"]=>
  string(0) ""
  ["Scheme"]=>
  string(4) "http"
  ["Segments"]=>
  {System.String[]}
  ["UserEscaped"]=>
  bool(false)
  ["UserInfo"]=>
  string(0) ""
  ["DnsSafeHost"]=>
  string(10) "google.com"
  ["IsAbsoluteUri"]=>
  bool(true)
  ["OriginalString"]=>
  string(17) "http://google.com"
}


weirdan
 
Posts: 17
Joined: June 4th, 2013, 1:11 am

Re: calling .net constructor from inheriting php class

Postby Jakub Misek » January 4th, 2016, 11:27 pm

I see, it is a bug ...
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 25 guests

cron

User Control Panel

Login

Who is online

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

Users browsing this forum: No registered users and 25 guests