Creating instance of class A in Class B

Discussion in 'PHP' started by CurlyDigital, Sep 19, 2009.

  1. #1
    Hi All,

    I am new to PHP, I am trying to create an instance of class one in class2, but I am not able to complete it.
    Please if any of you people can help, it will be of great help. Below is the code.



    class DataAccessLayer
    {
    private $connect_mysqli;

    public function __construct()
    {
    include 'CustomException.class.php';
    $this->DatabaseConnectionObject();
    }


    private function DatabaseConnectionObject()
    {
    try
    {

    include 'Config.class.php';
    $this->connect_mysqli = new mysqli($Config_HostName,$Config_UserName,$Config_Password,$Config_Database);

    if (mysqli_connect_errno())
    {
    printf("Connect failed: %s\n", mysqli_connect_error());
    }
    else
    {
    printf("Connect successful");
    }
    }
    catch(customException $ex)
    {
    $ex->ProcessErrorMessage();
    }
    }

    }

    --------------------------------------------------------------------------

    class BusinessLayer
    {

    private $objDataAccessLayer;

    public function __construct()
    {
    //include 'CustomException.class.php';
    $this->Initalize();
    }

    private function Initalize()
    {
    include 'DataAccessLayer.class.php';
    $this->$objDataAccessLayer = new DataAccessLayer();
    print 'tester';
    }

    }


    --------------------------------------------------------------------------

    Next is my html page


    include 'BusinessLayer.class.php';
    $newtest = new BusinessLayer();
    --------------------------------------------------------------------------

    It goes inside the line "$this->$objDataAccessLayer = new DataAccessLayer();" but it dosenot print the
    statement "print 'tester';". If you guys have some answers to this it is appreciated.

    Thanks
     
    CurlyDigital, Sep 19, 2009 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Try changing:

    $this->$objDataAccessLayer = new DataAccessLayer();

    To:

    $this->objDataAccessLayer = new DataAccessLayer();
     
    ThePHPMaster, Sep 19, 2009 IP
  3. CurlyDigital

    CurlyDigital Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey Grunt, Thanks Man.

    Very silly from me.
     
    CurlyDigital, Sep 20, 2009 IP