TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Help with classes (http://www.talkphp.com/absolute-beginners/1621-help-classes.html)

Swordbeta 12-05-2007 01:49 PM

Help with classes
 
I just followed some tutorials about classes in PHP and I made this:
PHP Code:

<?php

/**
 * @author Michael
 * @copyright 2007
 */
define("USER""test");
define("NAME""Michael");
define("PASS""mypass");

class 
MySQL {
    public function 
connect($db_user,$db_pass,$db_name) {
        
mysql_connect("localhost"$db_user$db_pass) or die(mysql_error());
        
mysql_select_db($db_name) or die(mysql_error());
    }
}
$connect = new MySQL();
$connect->connect(USER,PASS,NAME);

?>

Can someone tell me what I did wrong?

Karl 12-05-2007 01:59 PM

Seems fine, what error does it give?

Swordbeta 12-05-2007 02:02 PM

Hm...nvm,strange enough it works now! :D
Maybe the server had some delay or something.

SOCK 12-05-2007 04:00 PM

A bit of constructive criticism, if you don't mind. I understand that you're just starting out with classes and objects.

When you design a class, it should be functionally independent and fluid. You should consider not hard-coding anything like the server address / name into the class, and you should definitely never allow a class to kill a script or interact with the script outside itself.

A better way might be something more like this:
PHP Code:

class MySQL {

    
// variable to store an error message, or FALSE 
    
private $error FALSE;

    
// constructor - remember this cannot return a value!!
    
function __construct($db_host,$db_user,$db_pass,$db_name) {
        
$this->connect($db_host,$db_user,$db_pass);
        
$this->new_db($db_name);
    }

    
// connection method
    
public function connect($host,$user,$pass) {
        
mysql_connect($host,$user,$pass) OR $this->error mysql_error();
    }
    
// db selection method
    
public function new_db($db) {
        
mysql_select_db($db) OR $this->error mysql_error();
    }

    
// error handler
    
public function error_handler() {
        
// assumes no error has occurred
        
if ( $this->error == FALSE ) {
            return 
FALSE;
        } else {
            return 
$this->error;
        }
    }


So in the example class design, you have an error handler method that might be called from the script to check the status. Let the script determine what to do at that point. You might simply want it to log the error or send you an email.

Hope that helps, let me know if you have any questions.

Wildhoney 12-05-2007 05:08 PM

I understand what you mean, SOCKS, but if that were the case and you're not to hard-code anything into your classes, surely class constants would be a thing of the past? Are you saying that they are now unnecessary?

SOCK 12-05-2007 05:50 PM

Quote:

Originally Posted by Wildhoney (Post 5438)
I understand what you mean, SOCKS, but if that were the case and you're not to hard-code anything into your classes, surely class constants would be a thing of the past? Are you saying that they are now unnecessary?

No, class constants have their place. But creating a class where the host, user, password or other such data are hard-coded misses the point. The class should be portable and be flexible enough to be used anywhere.

Swordbeta 12-05-2007 06:30 PM

thanks for explaining sock :)
I got another problem...
It's something totally different,I'm making a file editor but problems with the login.
The pass and username are in the config.php,but when entering a wrong password it logs me in =/
PHP Code:

<?php

/**
 * @author Michael
 * @copyright 2007
 */
session_start();
include(
"config.php");
echo 
"<!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>
<title>Phatom Panel</title>
<style>
BODY { font-size: 11px; color: #7d7b7b;background-color:#1F1F1F; }
a:link, a:visited, a:active { text-decoration: none; color: #B9B9B9 }
a:hover { color: #D7D7D7; text-decoration:underline }
.table { border:1px solid #181818;text-align: center;background-color: #262626; }
</style>
</head>
<body>"
;
if(isset(
$_SESSION['pp_admin'])){
    if(
$_SESSION['pp_admin']!=$pass){
        unset(
$_SESSION['pp_admin']); 
    }
}
if(!isset(
$_GET['act'])){
    if(!isset(
$_SESSION['pp_admin'])){
    echo 
"<center>
    <table width='500' class='table'><tr>
    <td><h2><b>Login</b></h2></td>
    <td><form action='ppanel.php?act=admin' method='post'><p><br /><b>Admin Name:</b> <input type='text' name='name' /></p><p><b>Password:</b> <input type='password' name='pass' /></p><p><input type='submit' value='Login'></p></form>
    </td></tr></table></center>"
;
    }else if(isset(
$_SESSION['pp_admin'])){
        echo 
"<center><table width='500' class='table'><tr>
    <td><b>You're already logged in!</b><br /><a href='ppanel.php?act=panel'>Go to the Phantom Panel</a></td></tr></table></center>"
;
    }
}
if(
$_GET['act']=="admin"){
    
$a 0;
    if(isset(
$_POST['name']) && $_POST['name']!="" && isset($_POST['pass']) && $_POST['pass']!=""){
    if(
md5($_POST['pass'])==$pass){
        
$a 0;
    }else{
        
$a 1;
    }
    if(
$_POST['name']==$username){
        
$a 0;
    }else{
        
$a 1;
    }}
    echo 
"<center><table width='500' class='table'><tr><td>";
    if(
$a==0){
        
$_SESSION['pp_admin'] = $pass
        if(isset(
$_SESSION['pp_admin'])){
        echo 
"<b>Login Succesful!</b><br /><a href='ppanel.php?act=panel'>Go to the Phantom Panel</a>";
        }else{
        echo 
"<b>Unknown error.</b>";
        }
        }else{
                echo 
"<b>Admin Name/Password didn't matched.</b>";
        }
    echo 
"</center></td></tr></table>";
}
if(
$_GET['act']=="panel"){
        echo 
"<center><table width='500' class='table'><tr><td>";
    if(isset(
$_SESSION['pp_admin']) && $_SESSION['pp_admin']==$pass){
        echo 
"<font size='1'><a href='ppanel.php?act=logout'>Logout</a></font><h2>Welcome to your Phantom Panel!</h2><br /><br /><br /><img src='pp_images/edit.png' alt='' /> <a href='ppanel.php?act=edit&path=/'>File Editor</a><br /><br /><img src='pp_images/key.png' alt='' /><a href='ppanel.php?act=pass'>Change password</a>";
        }else{
        echo 
"<a href='ppanel.php'><h2>Please Login</h2></a>";    
        }
        echo 
"</td></tr></table>";
}
if(
$_GET['act']=="logout"){
    if(isset(
$_SESSION['pp_admin'])){
        unset(
$_SESSION['pp_admin']);
        echo 
"<center><table width='500' class='table'><tr><td><h2>Your now logged out</h2></td></tr></table</center>";
    }else{
        echo 
"<center><table width='500' class='table'><tr><td><a href='ppanel.php'><h2>Please login first.</h2></a></td></tr></table</center>";
    }
}
if(
$_GET['act']=="pass"){
        if(isset(
$_SESSION['pp_admin']) && $_SESSION['pp_admin']==$pass){
    if(!
$_POST){
    echo 
"<center><table width='500' class='table'><tr><td><h2>Change your password</h2><br /><br /><form action='ppanel.php?act=pass' method='post'><p><b>Current Password:</b> <input type='password' name='pass' /></p><p><b>New Password:</b> <input type='password' name='pass1' /></p><p><b>Confirm new password:</b><input type='password' name='pass2'></p><p><input type='submit' value='Change password'></form></td></tr></table</center>";
    }else if(
$_POST){
        if(
$_POST['pass'] && $_POST['pass1'] && $_POST['pass2']){
            if(
md5($_POST['pass'])==$pass && $_POST['pass1']==$_POST['pass2']){
                
$f "config.php";
                
$fh fopen($f'w') or die("can't open file");
                
$data "
                <"
."?php 
                \$username = \""
.$username."\";
                \$pass = \""
.md5($_POST['pass2'])."\"; 
                ?"
.">";
                
fwrite($fh$data);
            }else{
                echo 
"<center><table width='500' class='table'><tr><td><h2>Passwords didn't matched!</h2></td></tr></table</center>";
            }
        }else{
            echo 
"<center><table width='500' class='table'><tr><td><h2>Please fill in all fields!</h2></td></tr></table</center>";
        }
    }
    }else{
        echo 
"<center><table width='500' class='table'><tr><td><a href='ppanel.php'><h2>Please login!</h2></a></td></tr></table</center>";
    }
}
echo 
"<br /><br /><br /><br /><center>Phantom Panel copyright Michael of Phantom-designs.net</center></body></html>";
?>



All times are GMT. The time now is 03:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0