Hi,

I got a problem with class.

I've created 3 classes:
* identifiantsClass.php
* connectInfosClass.php
* sqlClass.php

In my third class, i always have the message: "error to connect" even if my database's name is good.

Please help me...

  • identifiantsClass.php

    class Identifiants{

    protected $serveur=null;
    protected $host=null;
    protected $mdp=null;
    protected $bdd=null;
    
    public function __construct(){
        $this->serveur=$serveur;
        $this->host=$host;
        $this->mdp=$mdp;
        $this->bdd=$bdd;
    } 
    
    public function identifiantsConnexion(){
        $this->serveur="nomServeur";
        $this->host='nomHote';
        $this->mdp='motdepasse';
        $this->bdd='bdd';
    
    }
    

    }

  • connectInfosClass.php

    class Connect extends Identifiants{

    protected $connection=null;
    protected $result=null;
    
    
    public function __construct(){
        $this->identifiantsConnexion();
    }
    
    public function connexion(){
        $this->connection=mysql_connect($this->serveur,$this->host,$this->mdp);
        if(!$this->connection){die;}
        $dataBase_connect = mysql_select_db($this->bdd,$this->connection);
        mysql_query("SET NAMES UTF8");
        if(!$dataBase_connect){$result=false;}else{$result=true;}
        return $result;
    }
    

    }

  • sqlClass.php

    class reqSql extends Connect{

        public function __construct(){
            $result=$this->connexion();
            if($result==false){
                echo("Error to connect to the database");
                exit;
            }
        }
    
        // Here it will be my sql requests      
    

    }

I do my test in a file :

<?php

include_once('identifiantsClass.php');
include_once('connectInfosClass.php');
include_once('sqlClass.php');


$testSql=new reqSql();


?>

Hi,

As far as I know, maybe I am wrong on this, but we cannot invoke the parent's parent's constructor in the grandchild's class... ( we could but it is really tricky,,, and that is not commonly used).

here is a simple enheritance with the invoked constructor in the child. WARNING! This class may have error. I just type it here without testing it. I don't have any chance to run it, because it is already late.

This is still normal

<?php
    class One{
     private $one = null;
     private $two = null;

     public function __construct($one,$two){

        $this->one = $one;
        $this->two = $two;
    }
    public function getOne(){
        return $this->one;
    }

    public function getTwo(){
        return $this->two;
    }
}
## end of class one
    ## child class two begins

    class Two extends One{
     private $three = null;

     public function __construct($one = null,$two = null,$three){
         parent::__construct($one,$two);
        $this->three = $three;
    }

    public function getThree(){
        $total = $this->three + (parent::getTwo());
        ## this should return integer 5
        return $total;
    }

    }
    ## end of class two



$newObject = new Two(1,2,3);

echo $newObject->getThree();

?>

Notice the above class Two it invokes the parent's constructor. Now, it will be too easy for anyone to assumed the class two can be extended, but wait a minute.. everything we have established in grandparent class will not be there in the grandchild.

For example, If I extend the child class above, it will become a parent and its parent will become the grandparent, so the arguments in the grandparents constructor is not going to be available in the grand child... plus I am not aware of any php function called get_grandparent_class as in get_parent_class referring to the parent class method.

Just to make our heads spin a little, let's extend the child class above and make it a parent, thus making its parent a grandparent.

 ## class three begins
    class Three extends  Two{
        public $four = null;

        public function __construct($one = null,$two = null, $three = null, $four){
           ## parent constructor below is just an assumption and not verified to be valid, because one and two are coming from the grandparent. the three is coming from the parent
            parent::__construct($one=null,$two=null,$three=null);
            $this->four = $four;
        }

        public function getFour(){
         ## this will return an error, because the getTwo belongs to the grandparent of this class
            $total = $this->four + (parent::getThree()) + (parent::getTwo());
            return $total;
        }
        }
    ## end of class three


$objectTwo = new Three(1,2,3,4);
echo $objectTwo->getFour();

If we put the class three below the class two above, we will get an error, because of get::getTwo() WHICH IS COMPLETELY WRONG and has no bearing of being valid even by the look of it, if it is called in the grandchild class..

In conclusion, you must create a well rounded class, and then extend only that class .. it can be extended by many class as long as it will only serve as a parent and not grandparent.

There are other ways of doing using the grandparent within the grandchild, but I don't even remember how it was done, but I am sure I have seen this some time ago.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.