Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9185173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:13:33+00:00 2026-06-17T19:13:33+00:00

I had 2 Class in PHP That i want to use with each other,

  • 0

I had 2 Class in PHP That i want to use with each other, but the Class in 2 different PHP Script like clothing_product.php and database.php. It look like this Below:

database.php:

require_once('config.php');

class MySqlDatabase
{
    private $connection;
    private $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exist;

        function __construct(){
            $this->open_connection();
            $this->magic_quotes_active = get_magic_quotes_gpc();
            $this->real_escape_string_exist = function_exists("mysql_real_escape_string");

        }

        private function open_connection()
        {

            $this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
            if (!$this->connection){
                die("Connection failed!". mysql_error());
            }
            else{
                $db_select = mysql_select_db(DB_NAME);
                if(!$db_select){
                    die("Select database failed". mysql_error());
                }
            }

        }

        public function query($sql){

            $this->last_query = $sql;
            $result = mysql_query($sql,$this->connection);

            $this->confirm_query($result);
            return $result;

        }

        public function confirm_query($result){
            if(!$result){
                $output = "Database query failed: " . mysql_error()."<br /><br />";
                $output.= "Last query that fail is:" . $this->last_query;
                die($output);
            }
        }

        private function escape_value($value) {

            if ($this->real_escape_string_exist) {
                if($this->magic_quotes_active) {$value = stripslashes($value);}
                $value = mysql_real_escape_string($value);
            }
            else {
                if (!$this->magic_quotes_active) {$value = addslashes($value);}
            }
            return $value;
        }

        public function fect_array($result){
            return mysql_fetch_array($result);
        }

        public function num_rows($result){
            return mysql_num_rows($result);
        }

        public function last_id(){
            return mysql_insert_id($this->connection);
        }

        public function affected_rows(){
            return mysql_affected_rows($this->connection);
        }

        public function close_connection(){
            if(isset($this->connection)){
                mysql_close($this->connection);
                unset($this->connection);
            }
        }

}

//$db = new MySqlDatabase();

clothing_product.php:

include(‘../database.php’);

class Clothing_Product {

    public $db = new MySqlDatabase();

        public static function test(){
            echo "Static call successfull";
            return "Static call successfull";
        }


    }

The problem is when i try to USE ‘Public $db = new MySqlDatabase();’ in class clothing_product i get Error. I think the problem is maybe i got a wrong call. Please help me cuz m a noob thnk.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:13:34+00:00Added an answer on June 17, 2026 at 7:13 pm

    You can’t initialize member variables to anything that is not static, and you’re trying to create an object here:

    public $db = new MySqlDatabase();
    

    From the manual:

    This declaration may include an initialization, but this
    initialization must be a constant value–that is, it must be able to
    be evaluated at compile time and must not depend on run-time
    information in order to be evaluated.

    The workaround is to set your variable in the constructor:

    public $db;
    public function __construct() { 
        $this->db = new MySqlDatabase();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i use a php class to make tag cloud from article, but i want
I have a class that had an inline member, but I later decided that
I have a cocoa class set up that I want to use to connect
The story: I had class User and class Organization: User. I did not use
I have an application that uses CoreData. I previously had a class named Marker
i had created a class with two properties one of datetime datatype and other
I've had my socket class working for a while now, but I wanted to
My php script puts an image with a hidden checkbox for that image. When
so I'm writing DataBase class which will be an encapsulation layer between PHP Controller
Im rewriting application from .NET to PHP. I need to create class like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.