$this keyword as an argument?

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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. }
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

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
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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')

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 64 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.