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
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Jun 08, 2008 6:05 pm
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 ..."
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Sun Jun 08, 2008 6:24 pm
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
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Jun 08, 2008 7:22 pm
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'
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Fri Jun 13, 2008 7:47 pm
I still can't get this working. Any ideas please!!
hansford
Forum Commoner
Posts: 91 Joined: Mon May 26, 2008 12:38 am
Post
by hansford » Fri Jun 13, 2008 8:56 pm
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Jun 13, 2008 9:04 pm
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)