Class returing Cannot re-assign $this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Class returing Cannot re-assign $this

Post by tecktalkcm0391 »

I am writing one of my first classes... or at least attempting...

I am trying to achieve the same thing in the class as below:

Code: Select all

function connect($dbc_name = $default){
    ...
}
This is what I have:

Code: Select all

<?php
class db {
    private $db_host = '.com';
    private $db_user = 'sandboxuser';
    private $db_pass = 'sandbox';
    private $db_name = 'sandbox';
    private $dbc_name;
 
    function connect($this->dbc_name = $this->db_host){
        $this->connectionLink = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        if($this->connectionLink){
            mysql_select_db($this->db_name, $this->connectionLink);
        }
    }
    
}
 
$test = new db();
$test->connect();
 
 
?>
I just can't get the code to work without returing "Fatal error: Cannot re-assign $this ..."
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Class returing Cannot re-assign $this

Post by VladSun »

You can't use $this as function parameter in the function definition. You need somtehing like this:

Code: Select all

function connect($db_host){
        $this->db_host = $db_host;
        $this->connectionLink = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        if($this->connectionLink){
            mysql_select_db($this->db_name, $this->connectionLink);
        }
    }
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Class returing Cannot re-assign $this

Post by tecktalkcm0391 »

Would this be the same as:

Code: Select all

<?php 
function connect($dbname = 'test'){
...
}
?>
Where if you do connect() the $dbname would be test and if you do connect('database') the $dbname would be 'database'
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Class returing Cannot re-assign $this

Post by tecktalkcm0391 »

I still can't get this working. Any ideas please!!
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Class returing Cannot re-assign $this

Post by hansford »

I agree with vladsun. Also, declare connectionLink; in the class but don't assign a value. you're getting errors b/c you're trying to assign values in the function(args) pass the args and do the assigning inside the function.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Class returing Cannot re-assign $this

Post by Christopher »

Typically I use a null default value when I want something like this:

Code: Select all

class db {
    private $db_host = '.com';
    private $db_user = 'sandboxuser';
    private $db_pass = 'sandbox';
    private $db_name = 'sandbox';
 
    function connect($db_name=null){
        if ($db_name !== null) {
            $this->db_name = $db_name;
        }
        $this->connectionLink = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        if($this->connectionLink){
            mysql_select_db($this->db_name, $this->connectionLink);
        }
    }
(#10850)
Post Reply