$this keyword as an argument?

  • RockmanTV
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Oct 12, 2004
  • Posts: 348
  • Status: Offline

Post April 28th, 2010, 9:49 am

I'm working on building up my library of classes and I'm writing a class that manages a site's hyperlinks. In this class I have a writeLink function that has an optional second argument to change how the link uses it's quotes for attributes. As you can see by default, the object sets the quotes to 'double'. I want to give the user of the class the authority to temporarily override this setting for individual writeLink calls.

I thought this could be performed by setting the $qt argument to $this->quotes which would then only be overridden if a user specifies otherwise. This doesn't seem to be the case however as I get a "Parse error: syntax error, unexpected T_VARIABLE" whenever I user the $this keyword for the argument. Any ideas?


Code: [ Select ]
private $quotes = 'double';

function writeLink($markup,$qt=$this->quotes){

}
  1. private $quotes = 'double';
  2. function writeLink($markup,$qt=$this->quotes){
  3. }
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 28th, 2010, 9:49 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post April 28th, 2010, 10:00 am

PHP Code: [ Select ]
function writeLink($markup, $qt = null)
{
    if(empty($qt)) $qt = $this->quotes;
}
  1. function writeLink($markup, $qt = null)
  2. {
  3.     if(empty($qt)) $qt = $this->quotes;
  4. }
Strong with this one, the sudo is.
  • RockmanTV
  • Proficient
  • Proficient
  • No Avatar
  • Joined: Oct 12, 2004
  • Posts: 348
  • Status: Offline

Post April 28th, 2010, 10:16 am

There's no way to do that in the methods' arguments?

I'm trying to make my script as compact as possible :) But I'm thinking you're right and I'll end up having to perform an if statement to check instead.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post April 28th, 2010, 11:40 am

Looks like you're limited to strings, arrays, and null, for default values.


There's a somewhat confusing note at the bottom of the manual page.

Quote:
Note: As of PHP 5, default values may be passed by reference.


I think that just means you can do something like this though.

PHP Code: [ Select ]
function f(&$var = 'f')
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: Zealous and 155 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.