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 6999523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:33:08+00:00 2026-05-27T20:33:08+00:00

I am creating 2 Class and from 1 class I am referring the other

  • 0

I am creating 2 Class and from 1 class I am referring the other class a PDO Object. But when I refer any string of that class, but not when PDO Object. Any ideas?
Here is my code

class Connection
    {
        private $dbcc;
        public function Fn_Db_Conn()
        {
            $this->dbcc = new  PDO( "mysql:host=localhost;dbname=db1;",
             "root","pass1");
            return $this->dbcc;
        }
    }
    class Registration
    {
        private $Username;
        private $dbc;
        public function Registration($Un)
        {
            $this->Username = $Un;
            $this->dbc = new Connection;
            $this->dbc->Fn_Db_Conn();
        }
        public function Fn_User_Exist()
        {

            $Qry = "SELECT * FROM CMT_Users WHERE Username=@Username";
            $Result = $this->dbc->prepare($Qry);
            $Result->bindParam("@Username",$this->Username);
            $Result->execute();
            print $Result->rowCount();
        }
    }
  • 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-05-27T20:33:09+00:00Added an answer on May 27, 2026 at 8:33 pm
    class Connection
    {
        private $_dbcc;
        public function getConnection()
        {
           return $this->_dbcc;
        }
        public function __construct()
        {
            $this->_dbcc = new  PDO( "mysql:host=localhost;dbname=db1;",
                 "root","pass1");
        }
    }
    class Registration
    {
        private $_username;
        private $_dbc;
    
    
        public function __construct($un)
        {
            $this->_username = $un;
            $this->_dbc = new Connection();
        }
        public function Fn_User_Exist()
        {
    
            $qry = "SELECT * FROM CMT_Users WHERE Username=@Username";
            $result = $this->_dbc->getConnection()->prepare($qry);
            $result->bindParam("@Username",$this->_username);
            $result->execute();
            print $result->rowCount();
        }
    }
    

    I have also modified the Connection class to create the PDO object in the constructor and added a getConnection method for accessing the PDO object.

    You should use the __construct keyword for constructor, naming the constructor as the class name is the old syntax and make the code more difficult to edit.

    Last point, it depends on people but I prefer to prepend protected and private properties or methods by an underscore _, this way we can identify easily if the method/property is accessible outside of the class or not. You should aboid using variable like Result because PHP is case sensitive so Result is not equal to result, so better to avoid typo to keep the variable name in lower case(appart when you want to do camelCase).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I'm creating a new class that inherits from UIView. I remember that xcode 3
I'm creating a class (say, C) that associates data (say, D) with an object
I am creating a class that derives from the WPF RichTextBox control and I
I'm creating a class that inherits from a parent class with protected instantiation. The
I'm creating a base class Node that essentially wraps instances of another class from
I want to fetch data from MySQL without creating object from class Normally I
I am creating a class that implements java.util.List . My class is using E
I'm creating a class that will have one public method, which returns a value
What is the use of creating base class object using child class reference in

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.