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
https://forums.phpfreaks.com/topic/216934-calling-a-class-from-within-a-class/
Share on other sites

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.

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

 

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?

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.