Jump to content

Recommended Posts

Hi Folks!

 

I have been designing basic sites / web applications for the last couple of years now but have recently decided that I really want to be able to code amazing sites, so have started a journey to teach myself to become a better programmer / coder. (so any pointers on books to read would be good) my skills so far... (Jquery, PHP (procedural), HTML / XHTML) so want to develop these further and also learn AJAX, PHP OOP.

 

On a daily basis i get on just fine with PHP writing almost all sites / apps in PHP ( Procedural ) but have decided that i should stop being lazy and actually learn to code OOP PHP, the only problem is on my first attemp i am completly stumped, i have wrote what i believe to be an extramly simple piece of code but for some reason it just does not work :wtf:.

 

Can one of you nice people please point out where the hell i am going wrong, i have written (see code below) a "class_lib.php" for my db connection etc and a simple html ("index.php") to display the results.

 

"CLASS_LIB.PHP"

<?php

class dbcon {
var $dbhost = NULL;
var $dbuser = NULL;
var $dbpass = NULL;
var $db = NULL;
var $con = NULL;

function setdb($host, $user, $password, $database) {
	$this->dbhost = $host;
	$this->dbuser = $user;
	$this->dbpass = $password;
	$this->db = $database;
	$this->con = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die (mysql_error());
	mysql_select_db($this->db) or die (mysql_error());	
}

function querydb($query) {
	return mysql_query($query, $this->con) or die (mysql_error());		  
}

}

?>

 

 

"INDEX.PHP"

 

<?php include("class_lib.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<p><?php 

$mysql = new dbcon();
$mysql->setdb('localhost','user','password','database');
$result = $mysql->querydb("SELECT * FROM datatable");

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$id = mysql_result($result,$i,"id");
$fname = mysql_result($result,$i,"fname");
$sname = mysql_result($result,$i,"sname");

echo nl2br("<div><b>$fname $sname</b><br /><br /><hr></div>");
$i++;
}


?></p>
</body>
</html>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/236002-trying-to-get-to-grips-with-oop/
Share on other sites

Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation.

 

I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason.

 

You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written.

 

PS: You might want to find a more up to date learning resource, your using PHP4 syntax.

what's the output?

 

nothing blank page,

 

also tested with putting "print_r($result);" into index.php to see what that output was and it just displayed the character "1" nothing else!! really odd, if i purposly enter an incorrect user or password it displays the error as expected so is using the class. :shrug:

Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation.

 

I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason.

 

You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written.

 

PS: You might want to find a more up to date learning resource, your using PHP4 syntax.

 

Thanks for the insightful words :D, you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck.

 

Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent?

 

oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!!  :D

Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation.

 

I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason.

 

You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written.

 

PS: You might want to find a more up to date learning resource, your using PHP4 syntax.

 

Thanks for the insightful words :D, you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck.

 

Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent?

 

oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!!  :D

 

The 'var' keyword is deprecated, and a clear sign of PHP 4.  You should be using one of the access modifiers - 'public', 'protected', or 'private' - instead.

 

PHP 5 has been around since 2004.  There's no reason not to get up to speed with it.  There is no PHP 6.  The key features that were slated for PHP 6 - namespaces and late static binding - have instead been incorporated in the latest version (or so) of PHP 5.

I have SOLVED the problem!!!!!!! :D :D :D :D :D

 

but would it be possible for someone to tell me why this worked

 

function querydb($query) {
	$results = mysql_query($query, $this->con) or die (mysql_error());
	return $results;
}

 

and this didn't, i dont get the difference

 

function querydb($query) {
	return mysql_query($query, $this->con) or die (mysql_error());
}

 

Database wrappers are just that, a wrapper around already existing functionality. You class adds no benefit to the current implementation.

 

I understand your likely just trying to learn the syntax but you need to go a little deeper than simply wrapping already existing functionality for no reason.

 

You class relies upon the mysql extension, have you though of actually adding flexibility to your design and allowing it to use any kind of database extension? This will give you a better idea of OOP design. Thought still, I would only use a db abstraction as a learning tool, in real life there are very good database abstraction layers already written.

 

PS: You might want to find a more up to date learning resource, your using PHP4 syntax.

 

Thanks for the insightful words :D, you are correct there is no point in what i am trying to do other than learn the syntax for OOP but other than REALLY basic stuff (like get / set) im stuck.

 

Interesting you say im using PHP4 syntax, i had no idea, i am completely self taught and as yet have never read a book on PHP (obviously i will do now, any suggestions for a good one?), just out of curiosity what part of my syntax is PHP4 and what is the PHP5 or even PHP6 equivalent?

 

oh p.s. has anyone actually got an answer to my actual problem, as i would love to know why it is not working!!  :D

 

The 'var' keyword is deprecated, and a clear sign of PHP 4.  You should be using one of the access modifiers - 'public', 'protected', or 'private' - instead.

 

PHP 5 has been around since 2004.  There's no reason not to get up to speed with it.  There is no PHP 6.  The key features that were slated for PHP 6 - namespaces and late static binding - have instead been incorporated in the latest version (or so) of PHP 5.

 

Thanks for that, i shall investigate the access modifiers a little more. as i say i am self taught and got the "var" thing from when i used to do VB programming, just found it worked dont think i actually picked it up from anywhere.

 

anyone know of any good books to get upto speed on PHP / PHP OOP as most i have looked at start from a REALLY basic / beginner point of view

anyone know of any good books to get upto speed on PHP / PHP OOP as most i have looked at start from a REALLY basic / beginner point of view

 

Sorry to say, but it looks to me like that is exactly where you should be starting from. However, Google the book PHP Objects, Patterns & Practice - it's a pretty good starting point.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.