Jump to content

Calling a class from within a class


jimmyt1988

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

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.