-3

i have this 3 class

"Fatal error: Call to undefined function bluesoft\connect() in C:\wamp\www\class\utility\DataBase.php on line 43"

1-database file

<?php


    namespace bluesoft;

     class DataBase {

    public  function  __construct()
    {
        echo "1";
    }


    public   function Connect()
            {
                $servername = "localhost";
                $username = "root";
                $password = "";

                try {
                    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
                    // set the PDO error mode to exception
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    return 1;
                }
                catch(PDOException $e)
                {
                    return 2;
                }


            }


    public function TestConnect()
           {
               if(!connect() == 1)
               {
                   echo"connect error";
               }

           }


} 


and

2-User file

<?php


    namespace bluesoft\User;
    use bluesoft\DataBase as am;
    include("DataBase.php");

    class User extends am{
    public  $User_id;
    private $User_name;
    private $User_email;
    private $User_pass;
    private $User_per;
    private $User_date;
    private $User_lldate;
    private $User_act;


    public function __construct()
    {

        $z= new am();
        $z->TestConnect();
    }


    public function add()
    {


    }

3- index

    <?php

include "class/utility/User.php";



$a = new bluesoft\User\User();
$a->User_id =1;

?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85

1 Answers1

0

connect() is not a global function so you need to use it with object reference so

public function TestConnect()
{
    if(!$this->connect() == 1)
    {
        echo"connect error";
    }
}

the $this is the current object.

jmattheis
  • 10,494
  • 11
  • 46
  • 58