Jump to content

[SOLVED] Variables defined in class, and accessed in another


NArc0t1c

Recommended Posts

Hello again,

 

I have a few classes written and saved as php scrips.

I'm not having trouble sending data between them, but I'm having a problem retrieving a variable form another class.

I'm sure this is possible, to get a variable from outside the class.

 

Here is a part of my one script, it holds the $Settings class.

<?php

if (!defined('Class')){
Echo 'Hacking Attempt..'; 
Exit;
}


Class Settings {

var $MySQLHost;
var $MySQLUserName;
var $MySQLPassword;
var $MySQLDataBase;

Public Function Data(){

	/* MySQL Information */		
	$this->MySQLHost = 'localhost';
	$this->MySQLUserName = 'root';
	$this->MySQLPassword = '';
	$this->MySQLDataBase = 'database';

}
/* Then I would have say a function to get the data, I have it in function's because some needs some more actions to be done to it. */
Function MySQLHost(){
	$Value = $this->MySQLHost;
	Return $Value;
}
/* And so on.. */
}

 

I also have a mysql class, It's a script I got from www.Zend.com in the code galaries.

I have removed most functions I do not need, This is the $MySQL class.

<?php

if (!defined('Class')){
Echo 'Hacking Attempt..'; 
Exit;
}

// constants used by class
define('MYSQL_TYPES_NUMERIC', 'int real ');
define('MYSQL_TYPES_DATE', 'datetime timestamp year date time ');
define('MYSQL_TYPES_STRING', 'string blob ');

class MySQL {

   var $last_error;         // holds the last error. Usually mysql_error()
   var $last_query;         // holds the last query executed.
   
   var $host;               // mySQL host to connect to
   var $user;               // mySQL user name
   var $pw;                 // mySQL password
   var $db;                 // mySQL database to select

   var $db_link;            // current/last database link identifier
   var $auto_slashes;       // the class will add/strip slashes when it can
   
   function db_class() {
   
      // class constructor.  Initializations here.
      
      // Setup your own default values for connecting to the database here. You
      // can also set these values in the connect() function and using
      // the select_database() function.
      
      $this->host = 'localhost';
      $this->user = 'root';
      $this->pw = '';
      $this->db = 'database_name';

      $this->auto_slashes = true;
   }

   function Connect($host='', $user='', $pw='', $db='', $persistant=true) {

      // Opens a connection to MySQL and selects the database.  If any of the
      // function's parameter's are set, we want to update the class variables.  
      // If they are NOT set, then we're giong to use the currently existing
      // class variables.
      // Returns true if successful, false if there is failure.  
      
      if (!empty($host)) $this->host = $host; 
      if (!empty($user)) $this->user = $user; 
      if (!empty($pw)) $this->pw = $pw; 


      // Establish the connection.
      if ($persistant) {
         $this->db_link = mysql_pconnect($this->host, $this->user, $this->pw); }
      else {
         $this->db_link = mysql_connect($this->host, $this->user, $this->pw); }

      // Check for an error establishing a connection
      if (!$this->db_link) {
         $this->last_error = mysql_error();
         return false; 
      } 
  
      // Select the database
      if (!$this->select_db($db)){ return false; }

      return true;  // success
   }

   function select_db($db='') {

      // Selects the database for use.  If the function's $db parameter is 
      // passed to the function then the class variable will be updated.

      if (!empty($db)){ $this->db = $db; }
      
      if (!mysql_select_db($this->db)) {
         $this->last_error = mysql_error();
         return false;
      }

      return true;
   }

}  

?>

 

In yet another class I would call the Connect Function with information from the $Settings class.

 

An example,

<?php
	$MySQL->Connect(
	/*   localhost  */ $Settings->MySQLHost(), 
	/*      root    */ $Settings->MySQLUsername(), 
	/*      none    */ $Settings->MySQLPassword());
	$MySQL->select_db(
	/*     narc0de  */  $Settings->MySQLDataBase());
?>

 

I have in the one script that I include for main pages:

<?php
    define('Class', time());
    
require("Classes/Settings.Class.php");
require("Classes/MySQL.Class.php");
      /* Assuming this is the previous script. */
require("Classes/Header.Class.php");

$Settings = new Settings();	
$MySQL = new MySQL();
$Header = new Header();
?>

 

Then I would make a page with a secure mysql connection by simply calling the classes.

<?php
include("Classes/Include.Class.php");
$Header->Start();
echo 'Hello World';
$Header->End();
?>

 

It's giving me the standard mysql error for no connection data, probably because the variables is empty.

What Am I doing wrong here, Missing something?

 

Edit: Made post little shorter, and less code.

 

Thanks

Ferdi

Link to comment
Share on other sites

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.