<?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); 

Recommended Answers

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 …

Jump to Post

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

Jump to Post

All 6 Replies

Member Avatar

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.

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

Member Avatar

since you did not pass any parameters to the constructor

I missed that bit :)

Perhaps my OOP article may shed some light on this.

Member Avatar

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

Be a part of the DaniWeb community

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