PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,154 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,616 people online right now. Registration is fast and FREE... Join Now!




Method Arguments

 

Method Arguments, Passing $this as an argument

teabag

17 Oct, 2009 - 12:18 PM
Post #1

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 7


My Contributions

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

<?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.
CODE

protected function Connect( $Host=$this->DATABASE['Host'], $User=$this->DATABASE['User'], $Pass=$this->DATABASE['Pass'], $DBase=$this->DATABASE['DBase'] )



User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 3)

CTphpnwb

RE: Method Arguments

17 Oct, 2009 - 12:38 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
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?
User is online!Profile CardPM
+Quote Post

teabag

RE: Method Arguments

17 Oct, 2009 - 12:40 PM
Post #3

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 7


My Contributions
QUOTE(CTphpnwb @ 17 Oct, 2009 - 12: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?


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.
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Method Arguments

17 Oct, 2009 - 12:55 PM
Post #4

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

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

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:12PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month