Jump to content


Photo

Object Question


  • Please log in to reply
6 replies to this topic

#1 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 07 April 2006 - 02:59 PM


is it possible? if is, how to do?



like this


function bar() {
  return "hello";
}

class foo {

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


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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 April 2006 - 03:29 PM

You can do this:
__construct($str = "hello")
You cannot call a function when when using parameters.

#3 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 07 April 2006 - 03:37 PM

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

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

  }
  public static function bar() {
      return "hello";
  }
}

[!--quoteo(post=362550:date=Apr 7 2006, 11:29 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 7 2006, 11:29 AM) View Post[/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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 April 2006 - 03:45 PM

No I meant when assigning parameters inside of functions ie:
function bar()
{
    return 'hello';
}

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

foo();

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

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.

#5 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 07 April 2006 - 03:52 PM

hmm....learn something everyday

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

  }
  public static function bar() {
      return "hello";
  }
}

notice it's still static

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#6 wolves

wolves
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 07 April 2006 - 04:53 PM

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

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

  }
  public static function bar() {
      return "hello";
  }
}

notice it's still static
[/quote]


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

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 April 2006 - 04:59 PM

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

This also applies to classes and methods too.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users