calling .net constructor from inheriting php class
7 posts
• Page 1 of 1
calling .net constructor from inheriting php class
I can't find a way to call .net constructor from inheriting php class:
Things I tried:
So how does one call the constructor?
- 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:
- parent::__construct($string); // errors shown above
- $this->Uri($string); // error PHP1240: Expecting parent constructor call
- parent::Uri($string); // error PHP1240: Expecting parent constructor call
- $this->{".ctor"}($string); // error PHP1240: Expecting parent constructor call
- parent::{".ctor"}($string); // error PHP2014: Syntax error: unexpected token '{'
- call_user_func([$this, ".ctor"], $string); // error PHP1240: Expecting parent constructor call
- call_user_func(["Uri", ".ctor"], $string); // error PHP1240: Expecting parent constructor call
- call_user_func(["Uri", "Uri"], $string); // error PHP1240: Expecting parent constructor call
- call_user_func(["Uri", "__construct"], $string); // error PHP1240: Expecting parent constructor call
- 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
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
Hi,
I have to dig a little into the Phalanger parser ... since some .NET limitations, there is an additional syntax for that.
Example:
In a future, we might remove this syntax extension.
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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: calling .net constructor from inheriting php class
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.
Thanks.
- weirdan
- Posts: 17
- Joined: June 4th, 2013, 1:11 am
Re: calling .net constructor from inheriting php class
you should be able to pass any expression into parents .ctor, so you can modify them e.g.weirdan wrote: so you can't modify parameters before they get to parent.
- Code: Select all
:parent($capacity * 10)
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: calling .net constructor from inheriting php class
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
I see, it is a bug ...
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 10 guests