Jump to content

[SOLVED] First time using classes.. Give me a break :)


Crew-Portal

Recommended Posts

<?php
class Site{
     // some stuff here.
}

class Connection extends Site{
   private $database;
   private $host;
   private $username;
   private $password;
   
   private function Connect(){
      $connection = @mysql_connect("$host", "$username", "$password") or die("Couldn't connect.");
      $db = @mysql_select_db($$database, $connection) or die("Couldn't select database.");
   }
   
   function Connection($db,$host,$user,$pass){
      this->$database = $db;
      this->$host = $host;
      this->$username = $user;
      this->$password = $pass;
      Connect();
   }
   
}

Site s = new Connection("bla","bla","bla","bla");
?>

It seems like it should work, However I end up getting this error:

 

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\lib\config.php on line 18

 

Any suggestions?

'$this', not 'this'

 


Site s = new Connection("bla","bla","bla","bla"); 

 

is also wrong.

 

$conn =  new Connection("bla","bla","bla","bla");

 

You might wanna look into constructors: http://us2.php.net/__construct

 

Why does the Connection class extends site class?

 

<?php

class Connection {
   
   function __construct($host, $username, $password, $database) {
      // This function gets called whenever you instantiate an object(example, $con = new Connection($u, $p, $h, $d); )
      $this->host = $host;
      $this->username = $username;
      $this->password = $password;
      $this->database = $database;

      // Call the function below.
      $this->Connect();
   }

   private function Connect(){
      $this->connection = @mysql_connect($this->host, $this->username, $this->password) or die("Couldn't connect.");
      $db = @mysql_select_db($this->database, $this->connection) or die("Couldn't select database.");
   }
   

   
}

$conn = new Connection("bla","bla","bla","bla");
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.