Jump to content

Object Question


wolves

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?
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.