0
<?php  

class contacts  
{  
    public $name;
    public  $cno;
    public  $clocation;


    public function __construct($name,$cno,$clocation) 
    {
        $this->name = trim($name);
        $this->cno  = trim($cno);
        $this->clocation= trim($clocation); 
    }

    public function getName()
    {
        return $this->name;
    }

    public function getNO()
    {
        return $this->cno;
    }

    public function getLocation()
    {
        return $this->clocation;
    }
}  

class DataBaseAction extends contacts 
{  
    private $db_info;
    private $db_user;
    private $db_pass;
    private $db;
    private $stmt;


    public  function __construct()
    {
        $this->db_info ='mysql:host=localhost;dbname=finalproject';
        $this->db_user ='root';
        $this->db_pass ='mrhot';
    }

    public  function insert_contact($sql)
    {
        $this->db = new PDO($this->db_info, $this->db_user, $this->db_pass);
        $this->stmt= $this->db->prepare($sql);
        $this->stmt->execute(array($this->getName(),$this->getNO(),$this->getLocation()));
        $this->stmt->closeCursor();
    }
} 

$obj = new DataBaseAction();

$l=$obj->getName();

var_dump($l); 
4
Contributors
6
Replies
25
Views
4 Years
Discussion Span
Last Post by diafol
2

Your 'child' construct is being used in preference to your parent construct. So this:

    $this->name = trim($name);
    $this->cno  = trim($cno);
    $this->clocation= trim($clocation); 

is never run. You could run the

parent:: __construct

before (or after) the lines in your child construct. To be honest, I don't know why you're running a child construct anyway - it's not doing anything useful as you could do this:

 private $db_info = 'mysql:host=localhost;dbname=finalproject';
 private $db_user = 'root';
 private $db_pass = 'mrhot';
 private $db;
 private $stmt;

If you get rid of the child construct and assign these variables as above, perhaps it will work.

Edited by diafol

2

name is not given a value yet, since you did not pass any parameters to the constructor, nor did you call the parent constructor.

0

since you did not pass any parameters to the constructor

I missed that bit :)

0

Doh! Not that video of you mooning the vicar's wife again!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.