jimmyt1988 Posted October 26, 2010 Share Posted October 26, 2010 Hi all, I have two classes. Registration and Connection. Inside a registration.php I include my header.php, which then includes my connection.php... So all the classes should be declared when the page is loaded. This is my code: registration.php: <?php include ('assets/header.php'); ?> <?php class registration{ public $fields = array("username", "email", "password"); public $data = array(); public $table = "users"; public $dateTime = ""; public $datePos = 0; public $dateEntryName = "date"; function timeStamp(){ return($this->dateTime = date("Y-m-d H:i:s")); } function insertRow($data, $table){ foreach($this->fields as $key => $value){ mysql_query("INSERT INTO graphs ($this->fields) VALUES ('$data[$key]')"); } mysql_close($connection->connect); } function validateFields(){ $connection = new connection(); $connection->connect(); foreach($this->fields as $key => $value){ array_push($this->data, $_POST[$this->fields[$key]]); } $this->dateTime = $this->timeStamp(); array_unshift($this->data, $this->dateTime); array_unshift($this->fields, $this->dateEntryName); foreach($this->data as $value){ echo "$value"; } $this->insertRow($this->data, $this->table); } } $registration = new registration(); $registration->validateFields(); ?> <?php include ('assets/footer.php'); ?> At this point I cannot find my connection class defined on another included/included page. $connection = new connection(); $connection->connect; config.php (included within header.php) <? class connection{ public $dbname = '**'; public $dbHost = '**'; public $dbUser = '**'; public $dbPass = '**'; public $connect; function connect(){ $this->connect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass) or die ('Error connecting to mysql'); mysql_select_db($this->dbname, $this->connect); } } ?> Any ideas how to call it properly? Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/ Share on other sites More sharing options...
Anti-Moronic Posted October 26, 2010 Share Posted October 26, 2010 From what I can tell you are calling it properly. It's hard to tell what the problem is without first seeing the other files involved and also what, if any, errors you are receiving. Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126882 Share on other sites More sharing options...
trq Posted October 26, 2010 Share Posted October 26, 2010 class $connection{ Should give you an error. class connection { is valid. Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126883 Share on other sites More sharing options...
jimmyt1988 Posted October 26, 2010 Author Share Posted October 26, 2010 Oh god its late, sorry.. Funny enough, it hasn't solved the problem ... can u see any more pathetic errors? anti mormic, ive updated my original post to help. Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126885 Share on other sites More sharing options...
PFMaBiSmAd Posted October 26, 2010 Share Posted October 26, 2010 Your need to use full php opening tags <?php in all your code. Edit: Also, the instance of your connection class is only available inside the validateFields() function, unless you assign that to an object of your registration class. Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126886 Share on other sites More sharing options...
jimmyt1988 Posted October 26, 2010 Author Share Posted October 26, 2010 Updated: registration-post.php: <?php include ('assets/header.php'); ?> <?php class registration{ public $fields = array("username", "email", "password"); public $data = array(); public $table = "users"; public $dateTime = ""; public $datePos = 0; public $dateEntryName = "date"; private $handle; function timeStamp(){ return($this->dateTime = date("Y-m-d H:i:s")); } function insertRow($data, $table){ foreach($this->fields as $key => $value){ mysql_query("INSERT INTO graphs ($this->fields) VALUES ('$data[$key]')"); } mysql_close($connection->connectData); } function validateFields(){ //Line 29 $this->handle = new connection(); $connection->connect(); foreach($this->fields as $key => $value){ array_push($this->data, $_POST[$this->fields[$key]]); } $this->dateTime = $this->timeStamp(); array_unshift($this->data, $this->dateTime); array_unshift($this->fields, $this->dateEntryName); foreach($this->data as $value){ echo "$value"; } $this->insertRow($this->data, $this->table); } } $registration = new registration(); $registration->validateFields(); ?> <?php include ('assets/footer.php'); ?> config.php: <?php class connection{ public $dbname = '**'; public $dbHost = '**'; public $dbUser = '**'; public $dbPass = '**'; public $connectData; function connect(){ $this->connectData = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass) or die ('Error connecting to mysql'); mysql_select_db($this->dbname, $this->connectData); } } ?> ERROR: Fatal error: Class 'connection' not found in \www\registration-post.php on line 29 Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126887 Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 I see. registration-post.php must include/require the file containing the class definition (config.php). we still haven't seen header.php yet... Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126889 Share on other sites More sharing options...
jimmyt1988 Posted October 26, 2010 Author Share Posted October 26, 2010 Omg, thankyou for asking that question. bad pathing. *hits head against table* thanks. ill sort the pathing out then come back and tell if its all ok. Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126892 Share on other sites More sharing options...
jimmyt1988 Posted October 26, 2010 Author Share Posted October 26, 2010 www/registration-post.php www/config.php www/assets/header.php The problem is in header.php. I am including the config.php with the pathing like this: <?php include('../config.php'); ?> But that isnt finding the config file.. even though its going back a directory (out of assets) and into the root (www) ? can you not do that with include function? Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126894 Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 i prefer full paths to includes: <?php include($_SERVER['DOCUMENT_ROOT'].'/config.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126895 Share on other sites More sharing options...
jimmyt1988 Posted October 26, 2010 Author Share Posted October 26, 2010 AND THEN IT WORKS. lol.. Goodness, i wish i knew what i did wrong. maybe my apache config was intefering with somerthing. the complete root now functions perfectly. THANKSSSSSS Quote Link to comment https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/#findComment-1126898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.