Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

OO PHP Help

Name: Anonymous 2009-07-19 8:20

I need some help learning Object Oriented PHP, and I am the type to learn from examples, so I was wondering if one of you kind gentleman could rewrite this file so that is OO.


$data['uname'] = "root";
$data['paswd'] = "";
$data['dhost'] = "localhost";
$data['dbase'] = "content";

function connect()
{
    global $data;

    $connection    = @mysql_connect(
        $data['dhost'],
        $data['uname'],
        $data['paswd']) or die("Error connectiong: " . mysql_error());

    @mysql_select_db($data['dbase'], $connection);
       
    return $connection;

}

function close($connection)
{
    mysql_close($connection);
}

function showAll()
{
    $connection = connect();
   
    $sql = "SELECT * FROM article";
    $result = mysql_query($sql);
   
    if ($result)
    {
        while ($row = mysql_fetch_array($result))
        {
            echo '<a href="/admin/index.php?id='.$row['id'].'">'.$row['title'].'</a> - <a href="/admin/edit.php?id='.$row['id'].'">EDIT</a> - <a href="/admin/admin.php?a=delete&id='.$row['id'].'">DELETE</a><br />';
        }
    }
   
    close($connection);
   
}

function getArticle($id)
{
    $connection = connect();
    $sql = "SELECT * FROM article WHERE id='$id'";
    $result = mysql_query($sql);
   
    if ($result)
    {
        $row = mysql_fetch_assoc($result);
        $articleinfo[] = $row;
    }
   
    return $articleinfo;
    close($connection);
   
}

Name: ha ha hahahha ha 2009-07-19 8:42

Object-Oriented PHP
Now you have 2 problems

Name: Anonymous 2009-07-19 8:50

   return $articleinfo;
   [b]close($connection);[/b]
Hello dead code.

Name: Anonymous 2009-07-19 8:58

function close($connection)
{
    mysql_close($connection);
}

( ≖‿≖)

Name: Anonymous 2009-07-19 12:09

>>1

i dont see how this is relevant to Fibonacci or factorial

Name: Anonymous 2009-07-19 16:25

>>2
This might surprise you, but it's actually 4 problems.

Name: Anonymous 2009-07-19 16:44

class foo
{
    var $data['uname'] = "root";
    var $data['paswd'] = "";
    var $data['dhost'] = "localhost";
    var $data['dbase'] = "content";
    var $conection;
       
   
    function __construct()
    {
        $this->connection    = @mysql_connect(
        $this->data['dhost'],
        $this->data['uname'],
        $this->data['paswd']) or die("Error connectiong: " . mysql_error());

        @mysql_select_db($this->data['dbase'], $cthis->onnection);
      }

    function __destruct()
    {
        mysql_close($this->connection);
    }

    function showAll()
    {
   
    $sql = "SELECT * FROM article";
    $result = mysql_query($sql,$this->connection);
  
        if ($result)
        {
            while ($row = mysql_fetch_array($result))
            {
                echo '<a href="/admin/index.php?id='.$row['id'].'">'.$row['title'].'</a> - <a href="/admin/edit.php?id='.$row['id'].'">EDIT</a> - <a href="/admin/admin.php?a=delete&id='.$row['id'].'">DELETE</a><br />';
            }
        }
 
    }

    function getArticle($id)
    {
        $sql = "SELECT * FROM article WHERE id='$id';
        $result = mysql_query($sql,$this->connection);
  
        if ($result)
        {
            $row = mysql_fetch_assoc($result);
            $articleinfo[] = $row;
        }
  
        return $articleinfo;
    }
}

Name: !3LrT5NRVks 2009-07-19 16:57

If you want to do OOP for a web page you should stick to java.  OOP in php leaves much to be desired.(and all other languages not designed from the ground to do OOP... i'm looking at you LISP)

Name: Anonymous 2009-07-19 17:01

>>8
You're saying that CLOS leaves something to be desired compared to Java? Oh, wow.

Name: Anonymous 2009-07-19 17:13

>>9
CLOS has one of the exact same critical flaws as Java: Primitives aren't objects.

Multi-dispatch is nice, though.

Name: !3LrT5NRVks 2009-07-19 17:41

>>9
Your suggesting that Lisp is not a toy language? Oh, wow.
This conversation is over.

Name: Anonymous 2009-07-19 17:56

>>10
object n. 1. any Lisp datum.
You're mixing up classes and objects. Classes are useful only when you wish to have an object that contributes to the structure and behavior of a set of objects. Being able to avoid them entirely is a feature.

Name: !P8jgul8W32 2009-07-19 18:14

>>11
Hey, you. Get away from my habit of occasionally using a trip with no name.

Name: Anonymous 2009-07-19 19:48

>>12
Haskell completely separates the concerns of defining types and assigning them to classes, so that any built-in type can always be added to a class. I'd like to see a Lisp object system based on that model.

Name: Anonymous 2009-07-19 20:26

>>14
Erm so how do you instantiate an object in Haskell if you don't know the constructors until....? Actually, come to think of it, I have no fucking clue what you're on about. I can only assume that Haskell is designed as the perfect language for insane trolls from the ground up

Name: Anonymous 2009-07-19 21:46

Gentle introduction to Haskell to the rescue
http://www.haskell.org/tutorial/classes.html

Name: Anonymous 2009-07-19 22:20

>>14,15
Where the fuck did this "Haskell is OOP" meme come from? Typeclasses have NOTHING TO DO with OO classes. Zero. Zilch. They're completely unrelated in every way possibly imaginable.

Name: Anonymous 2009-07-20 0:21

>>14
I don't know Haskell, but I think a type in CL is similar to a Haskell type class. Obviously neither apply to object systems.

Name: Anonymous 2009-07-20 0:25

it's not learning if someone else does it for you, fuck off

Name: Anonymous 2009-07-20 5:18

the solution to >>1 is:

require_once('DB.php');

Name: Anonymous 2009-07-20 6:41

Just use PDO

Name: Anonymous 2009-07-20 8:09

I have question (this one is not pertaining to OOP), but it is PHP related, and I didn't want to start another thread.

Would:

return ( $password == $data['password'] : return 1 );


Be the same as:

    if ($password == $data['password'])
    {
        return 0;
    }
    else
    {
        return 1;
    }


???

Name: Anonymous 2009-07-20 11:23


<?
$b = 'great';

if($b == 404) {
echo 'Where is my topic?';
} elseif($b < 404) {
echo 'In before the 404';
} else {
echo 'This stupid script does not work';
}

Name: Anonymous 2009-07-20 14:08

>>22

I don't think it is(not sure thought just test your code I am too busy). There are allso atleast two alternatives for that

return $password == $data['password'] ? 0 : 1;

or

return($password == $data['password']);

Name: Anonymous 2009-07-20 15:42

>>24
That would be return !($password == $data['password']);

Name: Anonymous 2010-12-20 22:09

Don't change these.
Name: Email:
Entire Thread Thread List