1

I'm very new to php constructors I know of from java. But for some reason, this one is not running. I am following a tutorial and I can't seem to get a 1 only a 0. Here are the two scripts involved:

<?php

class dbConnection{
    protected $db_conn;
    public $db_name = 'todo';
    public $db_user = 'root';
    public $db_pass = '';
    public $db_host = 'localhost';

    function connect(){
        try{
            $this->db_conn = new PDO("mysql:host=$this->db_host;db_name=$this->db_name",$this->db_user,$this->db_pass);
            return $this->db_conn;
        }
        catch(PDOException $e)
        {
            return $e->getMessage();
        }
    }

}

?>

<?php

include_once( 'class.database.php' );

class ManageUsers {
    public $link;
    public $constructed;

    function ManageUsers(){
        $db_connection = new dbConnection();
        $this->link = $db_connection->connect();
        return $this->link;
    }

    function registerUser($username,$password,$ip_address,$time,$date){
        $query = $this->link->prepare("INSERT INTO users (username,password,ip_address,time,date) VALUES (?,?,?,?,?)");
        $values = array($username,$password,$ip_address,$time,$date);
        $query->execute($values);
        $counts = $query->rowCount();
        return $counts;
    }
}


$users = new ManageUsers();

echo $users->registerUser('bob','bob','127.0.0.1','10:00','29-02-2012');
?>
Parsalin
  • 13
  • 4
  • Why do you think your constructor has not run? For what it's worth, $constructed will be null in your printout method, as that's a local scope variable. For it to be part of the object, both instances should be $this->constructed – Liam Wiltshire Jan 27 '14 at 15:36
  • Your code should be triggering several error messages. I think you haven't configured PHP to display errors messages and are coding blindly. That's something you need to fix before you go further; it's impossible to code properly without the aid of error messages. Here's a [brief explanation](http://stackoverflow.com/a/5680885/13508). – Álvaro González Jan 27 '14 at 15:39
  • And, regarding error handling, you shouldn't use the `try/catch` construct to downgrade exceptions to plain strings. That's true in Java as well. – Álvaro González Jan 27 '14 at 15:41
  • http://www.youtube.com/watch?v=yKaRjjHbPyE&list=PLA32F858B5A92DF2D This is the tutorial i am following and most likly this code is missing important things as i understand it its only enough to make a connection and echo an attempt to make a new user if a row is added then 1 should be printed. but my code and the code in the tutoiral have a difference somwhere... – Parsalin Jan 27 '14 at 15:48
  • />.< ok so i was attempting to make and declare a variable after a return of course it would be null. my constructer in fact runs the issue is else where so i would pour over it again thank you to everyone for all the great answers! – Parsalin Jan 27 '14 at 16:04

3 Answers3

1

You should try to use __construct() instead for ManageUsers().

Also, I'm not quite sure what you're trying to achieve in your constructor (see my markings below):

function ManageUsers(){
    $db_connection = new dbConnection();
    $this->link = $db_connection->connect();
    return $this->link; //<--?
    $constructed = 'Construct'; //<--?
}
karllindmark
  • 6,031
  • 1
  • 26
  • 41
  • Yes, i added that hoping at the end i could print out construct ther ebuti also tried echo and print in that location and nothing on the page the reason i think its the constructer is i have no syntax errors and i cannot ifnd a different in my code and the tutorials however i still cant get it to make a new user in mysql – Parsalin Jan 27 '14 at 15:40
  • Additionally, you shouldn't be returning anything from your constructor - you wouldn't do that in Java either AFAIK. :-) – karllindmark Jan 27 '14 at 15:44
1

Agreed - the method you are using is the old school PHP4 method - you should use the __construct() method as explained above.

http://www.php.net/manual/en/language.oop5.decon.php

This page explains construct() & destruct() in PHP 5

Zabs
  • 13,852
  • 45
  • 173
  • 297
  • at first i did have a __construct(){} but i found that php 5 and older can use this system so i tried it in hopes that was my issue. – Parsalin Jan 27 '14 at 15:43
0
// constructor
function __construct() {
 ManageUsers();
}


function ManageUsers(){
    $db_connection = new dbConnection();
    $this->link = $db_connection->connect();
    $constructed = 'Construct';
}

You cannot return from a constructor tho, maybe you can want to make link a public property and access it directly like this

$user = new ManageUsers();
$link = $user->$link;
meda
  • 45,103
  • 14
  • 92
  • 122