开发者

PHP: newbie question - something like with/end with in php?

开发者 https://www.devze.com 2023-02-17 17:44 出处:网络
is there something like with/end with (like in asp) for php? especially for class objects it would be nice - asp syntax is like:

is there something like with/end with (like in asp) for php? especially for class objects it would be nice - asp syntax is like:

with myWeb
    .init "myweb"
    response.write .h开发者_如何转开发tml  
end with

thanks


No, there is no such thing in PHP : you have to write the full name of classes / objects / variables / whatever when you want to use them.


No, AFAIK.

Do you really find this syntax useful ?


No, but there is an alternative syntax for control structures that may interest you.


not sure im right but tryed my best to translate your example :/

<?php function write_block(){
echo '.html';
}

die(write_block());
?>


It's not exactly what you wanted, but you can do something similar with PHP references:

<?php
class A {
    public $bar1 = 1;
    public $bar2 = 2;
    public $bar3 = 3;
}

class B {
    public $foo;
}

class C {
    public $foobar;
}

$myC = new C;
$myC->foobar = new B;
$myC->foobar->foo = new A;

print $myC->foobar->foo->bar1;
print $myC->foobar->foo->bar2;
print $myC->foobar->foo->bar3;

//Simpler with 'With...End With syntax:
//which might look something like:
//
// with ($myC->foobar->foo)         //Note this is not valid PHP
// {
//      print ->bar1;           //Note this is not valid PHP
//      print ->bar2;           //Note this is not valid PHP
//      print ->bar3;           //Note this is not valid PHP
// }
//
//Fortunately, you can sort of do this using an object reference:
//

$obj =& $myC->foobar->foo;
    print $obj->bar1;
    print $obj->bar2;
    print $obj->bar3;

unset ($obj);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号