Method Arguments

Passing $this as an argument

Page 1 of 1

3 Replies - 527 Views - Last Post: 17 October 2009 - 01:55 PM Rate Topic: -----

#1 teabag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 09

Method Arguments

Posted 17 October 2009 - 01:18 PM


Can you pass $this as an argument to a method?
Here is my code (not really useful, see the second code snippet):
<?php


abstract class Sql {
	
	private $DATABASE = array(	"Host" => Null,
								"User" => Null,
								"Pass" => Null,
								"DBase" => Null ); // Database information
	
	public function __construct( $Selector = "DATABASE00" ) // Select first database by default
	{
		
		// Buffer for mysql config file (with sections)
		if( $mysqlConfig = parse_ini_file( '../Config/Mysql.ini', true ) )
		{ 
			
			//Insert database information
			$this->DATABASE['Host'] = $mysqlConfig['AUTHENTICATE']['HOST'];
			$this->DATABASE['User'] = $mysqlConfig['AUTHENTICATE']['USER'];
			$this->DATABASE['Pass'] = $mysqlConfig['AUTHENTICATE']['PASS'];
			$this->DATABASE['DBase'] = $mysqlConfig['DATABASE'][$Selector];
			
		} else {
			
			die( "Could not find Mysql.ini!" );
			
		} // End Mysql.ini buffer
		
	}  // End __construct( );
	
	protected function Connect( $Host=$this->DATABASE['Host'], $User=$this->DATABASE['User'], $Pass=$this->DATABASE['Pass'], $DBase=$this->DATABASE['DBase'] )
{
		
		// Connect stuff here
		
	} // End Connect( );
	
}; // End class Sql
?>



On the line below, my editor is telling me I can't use the $this keyword in this sense.
The only other way to go about it is to give the arguments a default value of Null, and set in within the method.
protected function Connect( $Host=$this->DATABASE['Host'], $User=$this->DATABASE['User'], $Pass=$this->DATABASE['Pass'], $DBase=$this->DATABASE['DBase'] )




Is This A Good Question/Topic? 0
  • +

Replies To: Method Arguments

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Method Arguments

Posted 17 October 2009 - 01:38 PM

You don't need to pass anything to Connect(). Just use the database array values in the query. That's the point of setting them in the constructor, isn't it?
Was This Post Helpful? 0
  • +
  • -

#3 teabag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 23-June 09

Re: Method Arguments

Posted 17 October 2009 - 01:40 PM

View PostCTphpnwb, on 17 Oct, 2009 - 12:38 PM, said:

You don't need to pass anything to Connect(). Just use the database array values in the query. That's the point of setting them in the constructor, isn't it?


That's what it basically is. If they are not defined in the method call, then it defaults to the constructor's settings. I want a way to override the defaults though.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: Method Arguments

Posted 17 October 2009 - 01:55 PM

I suppose you could do something like:
  protected function Connect( $Host='', $User='', $Pass='', $DBase='')
	{
		if($Host != '' && $User != '' && $Pass != '' && $DBase != '')
		{
			// connect with new values
		} else
		{
			// connect with constructor values
		}
	}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1