Jump to content

[SOLVED] OOP question! Trying to write first class file


coldfiretech

Recommended Posts

Hello all, i started php programming about 2 weeks ago and have been directed to dive head first in to OOP, i have read the OOP tutorials here on the site and am trying to make my first class file..

I was working thru the tutorial and i cant figure out where to pass the login credentials to the mysqli function.. Like i said im a noob so.... could someone please show me how to do this..

 

class UserDataMapper   
{   
    private $_db;   
           
    public function __construct(mysqli $db)   
    {   
        $this->_db = $db;   
             //WHERES THE CREDENTIALS GO??
    }   
       
    public function find($username)   
    {   
        $result = $this->_db->query("SELECT * FROM user WHERE username = $username");   
           
        $row = $result->fetch_assoc();   
           
        return new User($row['username'], $row['password'], $row['logged_in']);   
    }   

It looks like you would do it like this:

$mysqli = new mysqli($host,$user,$password);

$userDataMapper = new UserDataMapper($mysqli);

 

So you instantiate mysqli object outside your class, and then pass it to constructor when creating an instance of UserDataMapper.

Okay i read that tutorial again and here is what i come up with and its not working  ???.

I am getting this error.

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\csx\testclass.php on line 19

 

TestClass.php

<?php
error_reporting(E_ALL);
	define("csx_host", "localhost");
	define("csx_user", "matt");
	define("csx_pass", "****");
	define("csx_db", "testdb");
	$mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db);
class csx_functions
{
private $_db;
function _construct($mysqli)	
	{
		$this->_db = $db;
	}	


public function test()
	{
	$result = $this->_db->query("SELECT login FROM USERS");
	$row = $result->FETCH_ASSOC();
	return $row;

	}	
}

?>

 

 

Index.php

<html>
<title>testing class files</title>
<?php include("testclass.php");?>
<body>

<?php
error_reporting(E_ALL);
$csx = new csx_functions();
$test = $csx->test();
echo $test;
?>

</body>
</html>

 

Yout index.php should look like this

 

<html>
<title>testing class files</title>
<?php include("testclass.php");?>
<body>

<?php
error_reporting(E_ALL);
$csx = new csx_functions($mysqli);   //here you're passing $mysqli object to $csx object, so that it could use database connection.
$test = $csx->test();
echo $test;
?>

</body>
</html>

I tried that and its still not working.

I am trying to make a class file with all my functions in it that connect to the database so i can do some validation and inserts and updates... things of that nature.

 

I havent done any oo programming before and i feel over my head right now.

For instance i would like to make a class to use to register a user into the database. But i dont really understand how i would set up the registration page itself to talk to that class and to tell it what action to do. and then

how to actually do the registration.  I mean i know how to connect to db and do inserts and what not but not like in oo... its like a whole new language!  I could really use some help, maybe a small example of how to do the registration thing, nothing fancy just a simple whatnot to get me understanding. Im just stuck right now, if i have to read that tutorial again the lump on my forehead is going to get larger! haha,   thanks!

 

Ok. So we've a working connection, but it doesn't get passed to your object.

 

Oh! I see it

 

The function should be called

__construct

not

 _construct

(two underscores, not one)

 

It's a special function that is called automatically, when you create a new object

Okay so i got rid of that error now i just cant get it to properly return data from the db.  I really feel like stuffing my head in the sand here!  OO is some seriously hard stuff to get down..  I guess i could be a little over anxious. I just started OOP 2 days ago.

 

testclass.php

<?php
error_reporting(E_ALL);
	define("csx_host", "localhost");
	define("csx_user", "stacks");
	define("csx_pass", "island67");
	define("csx_db", "cellsavior");
	$mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db);
class csx_functions
{
protected $_db;
function __construct($mysqli)
	{                            
		$this->_db = $mysqli;
	}



public function test($query)
	{
	$result = $this->_db->query($query);
	return $result->FETCH_ASSOC();
	echo $result;
	}	
}

?>

index.php

<html>
<title>testing class files</title>
<?php include("testclass.php");?>
<body>

<?php
error_reporting(E_ALL);
$csx = new csx_functions($mysqli);   //here you're passing $mysqli object to $csx object, so that it could use database connection.
$test = $csx->test("SELECT * FROM USERS");

?>

</body>
</html>

 

Your 'test' method uses mysqli extension in a wrong way.

 

See manual for information and examples.

 

 

You could use something lie this for example.

 

Try to figure out, why this way, and what are the functions used.

 

public function test($query)
{
$result = $this->_db->query($query);
while ($row = $result->fetch_assoc()) {
	print_r($row);
}
}

Okay so here is what i have come up with...  Cant get it to work...

Dont know what im doing wrong...

 

Its giving me this error

Notice: Undefined property: csx_functions::$db in C:\wamp\www\matt\testclass.php on line 20

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\matt\testclass.php on line 20

 

index.php

<?php
error_reporting(E_ALL);
$csx = new csx_functions($mysqli);   //here you're passing $mysqli object to $csx object, so that it could use database connection.
$result = $csx->check_email("[email protected]");
if ($result > 0) 
{
	//username is good to use
	echo "no good";
}else{
		//username is no good
		echo "good";

}
?>

 

 

testclass.php

<?php
error_reporting(E_ALL);
	define("csx_host", "localhost");
	define("csx_user", "matt);
	define("csx_pass", "*****);
	define("csx_db", "mydb");
	$mysqli = new mysqli(csx_host, csx_user, csx_pass, csx_db);

class csx_functions
{
protected $_db;
function __construct($mysqli)
	{                            
		$this->_db = $mysqli;
	}
public function check_email($email)
{

$query = "SELECT `email` from users WHERE email = '$email'";
$r = $this->db->query($query);
	if(mysqli_num_rows($r) >0){
		RETURN 0;
	}else{
		RETURN 1;
	}

}
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.