PDA

View Full Version : PHP Gurus...



Hitman
25-09-2009, 06:09 PM
Hey, I need somebody who's good at PHP to explain something to me. I can code alright PHP, like a usersystem or whatever, but the code tends to be messy/not very efficient.

I was looking at OOP... using classes and objects... I have no idea what they are. I looked at a few tuts and explanations and they went right over my head. :( I really don't understand them. Another thing is functions. I know that a function contains something and you can call that function up but I'm just as confused as to why it's helpful.

If anybody could explain I'd be immensely grateful.

Thanks.

BoyBetterKnow
25-09-2009, 08:11 PM
Mm. I find it helpful mainly due to the fact everything can extend each other if needed but I like private variables the most. They have come in so handy when I've been doing template classes.

For instance, if I was to do:

$template = new template('templateName');

then on __construct aka public function __construct I would store the templateName as a private variable yeh.

Then I could do stuff like

$template->setHeader('Title of Page');
$template->setContent('Content of page');
$template->setFooter('Footer of Page');
$template->setNavigation('Home', 'About', 'News', 'Blog');

And perhaps the navigation pages are db driven or w/e

Then setHeader, setContent, setFooter, setNavigation would be functions in the template class.

Then you would have

private $templateName;
private $templateHeader;
private $templateContent ;
private $templateFooter;
private $templateNavigation;

inside the template class.

Then at the end you can do

$template->render();

and it would take in all the private variables, set by the page with them simple functions and load the correct template file etc. The render would do the str_replace or w/e.

So it's this


<?php

include 'template.class.php';

$template->setHeader('Title of Page');
$template->setContent('Content of page');
$template->setFooter('Footer of Page');
$template->setNavigation('Home', 'About', 'News', 'Blog');

?>

VS.


<?php

include 'functions.php';

template('Title of Page', 'Content of Page', 'Footer of page', array(cba to do array);

?>

or you could do seperate procedural functions to get the parts of the template out.

There is so many more things you could then do with the template stuff or w/e. OOP is just better. Learn it slowly. I used to use it solely for organization purposes but there are many more bonus'. I am really not the right person to explain but I tried.

Hitman
26-09-2009, 08:16 AM
That's all well and good but I don't understand hardly any of it! :( I don't actually understand how it works, what the classes do, etc.

But thanks for replying.

BoyBetterKnow
26-09-2009, 10:11 AM
Right. For example.

We have an init file.


<?php

include_once 'database.core.php';

$database = new database();

?>

Now, I could change that around and have this.



<?php

// Include configuration file.
include_once 'config.php';

include_once 'database.core.php';

$database = new database($dbHost, $dbUsername, $dbPassword, $dbName);



and have my config file like this



<?php

$dbHost = 'localhost';
$dbUsername = 'Username';
$dbPassword = 'Password';
$dbName = 'dbname';


Then this part in our init.php file:

$database = new database($dbHost, $dbUsername, $dbPassword, $dbName);

That is basically saying

$database = new database('localhost', 'Username', 'Password', 'dbname');

as you probably know. So it is creating our database class and sending variables to it.

Now, to handle those variables, I would do something along the lines of:


<?php

class database {

function __construct($dbHost, $dbUsername, $dbPassword, $dbName){

// Mysql connection jazz goes here using them connection details.

}

}

__construct is basically run when you do new object.

So in this case I do new database in my php which tells the script or w/e to check for a construct in my database class. You have done functions before so you match them yeh and you can connect using them. Doing $database = new database() blah is a nice easy way of doing it. You can then do things such as set the links for queries to use and theres alot you can do but I'm not going into detail.

Blob
26-09-2009, 11:37 AM
Right. For example.

We have an init file.


<?php

include_once 'database.core.php';

$database = new database();

?>

Now, I could change that around and have this.



<?php

// Include configuration file.
include_once 'config.php';

include_once 'database.core.php';

$database = new database($dbHost, $dbUsername, $dbPassword, $dbName);



and have my config file like this



<?php

$dbHost = 'localhost';
$dbUsername = 'Username';
$dbPassword = 'Password';
$dbName = 'dbname';


Then this part in our init.php file:

$database = new database($dbHost, $dbUsername, $dbPassword, $dbName);

That is basically saying

$database = new database('localhost', 'Username', 'Password', 'dbname');

as you probably know. So it is creating our database class and sending variables to it.

Now, to handle those variables, I would do something along the lines of:


<?php

class database {

function __construct($dbHost, $dbUsername, $dbPassword, $dbName){

// Mysql connection jazz goes here using them connection details.

}

}

__construct is basically run when you do new object.

So in this case I do new database in my php which tells the script or w/e to check for a construct in my database class. You have done functions before so you match them yeh and you can connect using them. Doing $database = new database() blah is a nice easy way of doing it. You can then do things such as set the links for queries to use and theres alot you can do but I'm not going into detail.

If your loading the config before setting the class up, you might aswell just do this...

Config

<?php
$dbInfo["username"] = "hi";
$dbInfo["password"] = "dontlooks";
$dbInfo["server"] = "localhost";
$dbInfo["database"] = "hi";
?>

Init

<?php
require("Configs/Something.php");
require("Class/Something.php");

$class = new Thing( $dbInfo );
?>

Class

<?php
class Thing
{
private $dbInfo;

public function __construct( $dbInfo )
{
$this->dbInfo = $dbInfo;
$this->connectDatabase();
}

private function connectDatabase()
{
mysql_connect( $this->dbInfo["username"], etc... );
}
}
?>

BoyBetterKnow
26-09-2009, 12:30 PM
If your loading the config before setting the class up, you might aswell just do this...

Config

<?php
$dbInfo["username"] = "hi";
$dbInfo["password"] = "dontlooks";
$dbInfo["server"] = "localhost";
$dbInfo["database"] = "hi";
?>Init

<?php
require("Configs/Something.php");
require("Class/Something.php");

$class = new Thing( $dbInfo );
?>Class

<?php
class Thing
{
private $dbInfo;

public function __construct( $dbInfo )
{
$this->dbInfo = $dbInfo;
$this->connectDatabase();
}

private function connectDatabase()
{
mysql_connect( $this->dbInfo["username"], etc... );
}
}
?>

Mmm. Indeed I could. But does it really matter? No.. :P

Protege
26-09-2009, 01:30 PM
class database
{
public $core;

public function connect()
{
$dbConnect = ( bool ) mysql_connect( $this->core->__dbHost, $this->core->__dbUsername, $this->core->__dbPassword );

if( $dbConnect !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'DbBadConnect' );
return FALSE;
}

$dbSelectDb = ( bool ) mysql_select_db( $this->core->__dbDatabase );

if( $dbSelectDb !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'dbBadDatabase' );
return FALSE;
}

return TRUE;
}
/* ...... */
}


ya can do that instead if you want to.

BoyBetterKnow
26-09-2009, 01:44 PM
class database
{
public $core;

public function connect()
{
$dbConnect = ( bool ) mysql_connect( $this->core->__dbHost, $this->core->__dbUsername, $this->core->__dbPassword );

if( $dbConnect !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'DbBadConnect' );
return FALSE;
}

$dbSelectDb = ( bool ) mysql_select_db( $this->core->__dbDatabase );

if( $dbSelectDb !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'dbBadDatabase' );
return FALSE;
}

return TRUE;
}
/* ...... */
}
ya can do that instead if you want to.

Loool you're telling someone who has never used OOP to use that :P

Protege
26-09-2009, 02:06 PM
pretty straight forward

Blob
26-09-2009, 03:24 PM
class database
{
public $core;

public function connect()
{
$dbConnect = ( bool ) mysql_connect( $this->core->__dbHost, $this->core->__dbUsername, $this->core->__dbPassword );

if( $dbConnect !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'DbBadConnect' );
return FALSE;
}

$dbSelectDb = ( bool ) mysql_select_db( $this->core->__dbDatabase );

if( $dbSelectDb !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'dbBadDatabase' );
return FALSE;
}

return TRUE;
}
/* ...... */
}


ya can do that instead if you want to.

I use this to connect:

class MySQL {
public $kernel, $storedFetch;
private $dbInfo, $dbConnection, $db, $debugConnection, $debugInfo, $storedQuery;

public function __construct( $kernel )
{
$this->Kernel = $kernel;
$this->dbInfo = $this->Kernel->_loadItem( "MySQL", "MySQL.php", "Database/", "Config" );
$this->_bootDatabase();
}

private function _bootDatabase( )
{
if( empty( $this->dbInfo["Database"] ) !== false )
{
$this->Kernel->alert->displayAlert( "warning", 1003 );
return false;
}

$dbInfo["Username"] = ( empty( $dbInfo["Username"] ) !== false ) ? "root" : $dbInfo["Username"];
$dbInfo["Server"] = ( empty( $dbInfo["Server"] ) !== false ) ? "localhost" : $dbInfo["Server"];

$this->dbConnection = @mysql_connect( $this->dbInfo["Server"], $this->dbInfo["Username"], $this->dbInfo["Password"] );
$this->db = @mysql_select_db( $this->dbInfo["Database"] );

if( $this->dbConnection === false || $this->db === false )
{
// Can't connect! Lets try to debug..
$this->_debugConnection();
}
}

Agnostic Bear
26-09-2009, 07:53 PM
I use this to connect:

class MySQL {
public $kernel, $storedFetch;
private $dbInfo, $dbConnection, $db, $debugConnection, $debugInfo, $storedQuery;

public function __construct( $kernel )
{
$this->Kernel = $kernel;
$this->dbInfo = $this->Kernel->_loadItem( "MySQL", "MySQL.php", "Database/", "Config" );
$this->_bootDatabase();
}

private function _bootDatabase( )
{
if( empty( $this->dbInfo["Database"] ) !== false )
{
$this->Kernel->alert->displayAlert( "warning", 1003 );
return false;
}

$dbInfo["Username"] = ( empty( $dbInfo["Username"] ) !== false ) ? "root" : $dbInfo["Username"];
$dbInfo["Server"] = ( empty( $dbInfo["Server"] ) !== false ) ? "localhost" : $dbInfo["Server"];

$this->dbConnection = @mysql_connect( $this->dbInfo["Server"], $this->dbInfo["Username"], $this->dbInfo["Password"] );
$this->db = @mysql_select_db( $this->dbInfo["Database"] );

if( $this->dbConnection === false || $this->db === false )
{
// Can't connect! Lets try to debug..
$this->_debugConnection();
}
}

Ok that's it I am never letting you view my code over teamviewer again.



class Database
{
private $kernel;
private $_dbInfo;

private $db;

public function __construct( Kernel $kernel )
{
$this->kernel = $kernel;
$this->_startDbConn();
}

private function _startDbConn()
{
# Grab the configuration info
global $mysql;

$this->_dbInfo = $mysql;
unset( $mysql );

$class = ( strtolower( $this->_dbInfo[ 'driver' ] ) === 'mysqli' ) ? 'clientMySQLi' : 'clientMySQL' ;

$this->kernel->loadClass( $class, $class . '.php', '/Database' );
$this->db = $this->kernel->getClass( $class );

$this->db->connect( $this->_dbInfo );

$this->kernel->setClass( 'db', $this->db );
//print_r($this->_dbInfo);
}
}




class clientMySQL
{
private $kernel;

private $_conn;

public function __construct( $kernel )
{
$this->kernel = $kernel;
}

public function connect( $sql )
{
$persistent = ( $sql[ 'persistent' ] === 1 ) ? 'mysql_pconnect' : 'mysql_connect';

$host = ( isset( $sql[ 'port' ] ) === true and ctype_digit( $sql[ 'port' ] ) === true ) ? $sql[ 'hostname' ] . ':' . $sql[ 'port' ] : $sql[ 'hostname' ];

$this->_conn = @$persistent( $host, $sql[ 'username' ], $sql[ 'password' ] );

if( $this->_conn === false ) {
errHandler::mysqlError( mysql_errno() );
}
}
}

Blob
26-09-2009, 08:02 PM
Ok that's it I am never letting you view my code over teamviewer again.



class Database
{
private $kernel;
private $_dbInfo;

private $db;

public function __construct( Kernel $kernel )
{
$this->kernel = $kernel;
$this->_startDbConn();
}

private function _startDbConn()
{
# Grab the configuration info
global $mysql;

$this->_dbInfo = $mysql;
unset( $mysql );

$class = ( strtolower( $this->_dbInfo[ 'driver' ] ) === 'mysqli' ) ? 'clientMySQLi' : 'clientMySQL' ;

$this->kernel->loadClass( $class, $class . '.php', '/Database' );
$this->db = $this->kernel->getClass( $class );

$this->db->connect( $this->_dbInfo );

$this->kernel->setClass( 'db', $this->db );
//print_r($this->_dbInfo);
}
}




class clientMySQL
{
private $kernel;

private $_conn;

public function __construct( $kernel )
{
$this->kernel = $kernel;
}

public function connect( $sql )
{
$persistent = ( $sql[ 'persistent' ] === 1 ) ? 'mysql_pconnect' : 'mysql_connect';

$host = ( isset( $sql[ 'port' ] ) === true and ctype_digit( $sql[ 'port' ] ) === true ) ? $sql[ 'hostname' ] . ':' . $sql[ 'port' ] : $sql[ 'hostname' ];

$this->_conn = @$persistent( $host, $sql[ 'username' ], $sql[ 'password' ] );

if( $this->_conn === false ) {
errHandler::mysqlError( mysql_errno() );
}
}
}


Actually, I coded that without seeing your MySQL Class ever, its only your Kernel I have seen.

Protege
26-09-2009, 09:34 PM
Oh, I hear my roflcopter engine startin', come aboard har har har.

Ontopic: OOP is quite easy to get the hang of it, when you understand the basics my friend. You should check out php.net.

I wrote that, so I didn't get an infraction, BUT I SEE ONE COMING

Agnostic Bear
27-09-2009, 03:12 PM
Why are you typecasting everything? It's completely unnecessary.




class database
{
public $core;

public function connect()
{
$dbConnect = ( bool ) mysql_connect( $this->core->__dbHost, $this->core->__dbUsername, $this->core->__dbPassword );

if( $dbConnect !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'DbBadConnect' );
return FALSE;
}

$dbSelectDb = ( bool ) mysql_select_db( $this->core->__dbDatabase );

if( $dbSelectDb !== TRUE )
{
$this->core->___errorMessage = ( string ) $this->core->languages->___language( 'dbBadDatabase' );
return FALSE;
}

return TRUE;
}
/* ...... */
}


ya can do that instead if you want to.

Protege
27-09-2009, 08:16 PM
Bcuz its good practice... even though PHP auto interprets it all.

Invent
27-09-2009, 09:08 PM
Bcuz its good practice... even though PHP auto interprets it all.

It's only good practice in certain circumstances..not when the returned value is always going to be a specific data type.

Protege
28-09-2009, 05:00 AM
And when I've used it, the values could return a different type than specified.

Agnostic Bear
28-09-2009, 08:35 PM
And when I've used it, the values could return a different type than specified.


$dbConnect = ( bool ) mysql_connect( $this->core->__dbHost, $this->core->__dbUsername, $this->core->__dbPassword );
Useless, it will return boolean false if there's an error so you typecast it then check to make sure it's not true :eusa_thin


$dbSelectDb = ( bool ) mysql_select_db( $this->core->__dbDatabase );
Useless, it will always return a boolean type :eusa_thin

Protege
28-09-2009, 08:46 PM
I agree on the second part but on the first I have a mixed opinion.

However Ive used an if statement to check that it isn't true, so it is useless in that instance, unless I was to check it was equal to true it would be the correct way.

BoyBetterKnow
28-09-2009, 08:48 PM
This isn't really helping the thread starter. This is way too complex for a begginner. Seriously.

Shox
30-09-2009, 02:16 AM
That's all well and good but I don't understand hardly any of it! :( I don't actually understand how it works, what the classes do, etc.

But thanks for replying.

No one is answering what you are really asking.

Classes are just code representations of OBJECTS

let's say i had a class called CAR the whole class car is a OBJECT.

Classes have constructors (functions), and fields (variables), like a constructor in a car object would be

StartEngine()


Some fields in the object car would be:

Color
Model

That make things any clearer?

Why are some of you pasting code right and left? That's not explaining anything.

Want to hide these adverts? Register an account for free!