Jump to content

Archived

This topic is now archived and is closed to further replies.

wolves

Object Question

Recommended Posts


is it possible? if is, how to do?



like this

[code]

function bar() {
  return "hello";
}

class foo {

  public function __construct($str = bar()) {
  
        echo $str;  

  }

}

[/code]

it's not work, but have other syntax to do it?

Share this post


Link to post
Share on other sites
You can do this:
[code]__construct($str = "hello")[/code]You cannot call a function when when using parameters.

Share this post


Link to post
Share on other sites
I would think that would work at first glance, but class integration with a regular PHP file is kinda hectic anyway

I would say if it doesn't work, you could *grits teeth* make it global
most of the time it's not a good idea to make things global but if it works it works

and the best alternative I could think of would be to make the function bar just a static method in your class
but that just depends on how much bar() has to do with the class at all

see example
[code]

class foo {
  public function __construct($str = foo::bar()) {
  
        echo $str;  

  }
  public static function bar() {
      return "hello";
  }
}[/code]

[!--quoteo(post=362550:date=Apr 7 2006, 11:29 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 7 2006, 11:29 AM) [snapback]362550[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You cannot call a function when when using parameters.
[/quote]
are you sure....I've never ran into such a problem as what wolves is doing
but I would think that you could very well use a function to declare an argument value
it works for any other function
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
echo date("g:m", [!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]strtotime()[!--colorc--][/span][!--/colorc--])
[/quote]

but maybe classes are a different story I dunno...I'm not on my own machine

Share this post


Link to post
Share on other sites
No I meant when assigning parameters inside of functions ie:
[code]function bar()
{
    return 'hello';
}

//you cant do this:
function foo($str=bar())
{
    echo $str;
}

foo();

//But you can do this.
foo(bar());
[/code]

I know you can use functions when you are parsing parameters to functions , but not when you are assigning defualt values for parameters in side a function.

Share this post


Link to post
Share on other sites
hmm....learn something everyday

well I guess you'd have to do this then wolves
[code]
class foo {
  public $string = foo::bar();
  public function __construct($str = $this->string) {
  
        echo $str;  

  }
  public static function bar() {
      return "hello";
  }
}[/code]

notice it's still static

Share this post


Link to post
Share on other sites
[!--quoteo(post=362560:date=Apr 7 2006, 10:52 AM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Apr 7 2006, 10:52 AM) [snapback]362560[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hmm....learn something everyday

well I guess you'd have to do this then wolves
[code]
class foo {
  public $string = foo::bar();
  public function __construct($str = $this->string) {
  
        echo $str;  

  }
  public static function bar() {
      return "hello";
  }
}[/code]

notice it's still static
[/quote]


get unexpected '(', expecting ',' or ';', think it's not work

Share this post


Link to post
Share on other sites
You cant to do that either. You cannot call or use a function when delacaring parameters in a function:
[code]funcion foo($var=bar())[/code] or:
[code]$val = bar();
function($var=$val)[/code]
The only way to set a defualt value of a parameter in a function is to type it in manually ie:
[code]fucntion foo($var="hello")[/code]

This also applies to classes and methods too.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.