Jump to content

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


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");
?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.