PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Oct 01, 2020 8:02 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Aug 22, 2012 2:06 am 
Offline
Forum Newbie

Joined: Wed Aug 22, 2012 1:48 am
Posts: 1
Hi guys!

I've been designing quite low-usage websites, but now I've given task to design website service with potentially higher usage. I'm not so familiar with Object-Oriented PHP programming, so I'd like to ask simple best practice -question. It's more theoretical question.

I have written some PHP class libraries for my use. For example, I have one for login management, one for MySQL handling etc. etc. Now I have used this kind of approach, and I'm wondering if it's totally wrong when it comes to performance or security.

Example scenario:

Syntax: [ Download ] [ Hide ]
class MySQL {
    public function __construct(...) {  
       ...
    }

    function connect() {               
       ...
    }
       
    function select_db() {     
        ...
    }
       
    function query($qry) {
       ...
    }
}
 


Syntax: [ Download ] [ Hide ]
class other {
    public function __construct(...,$dbconn) {  
       $this->mysql = $dbconn;
    }

    function do_some_magic() {
      $this->mysql->query("TRUNCATE DATABASE xxx");
      ...
    }
}
 


Syntax: [ Download ] [ Hide ]

<?php

require_once 'inc/class.user.php';
require_once 'inc/class.other.php';

$mysql = new MySQL(...);

$mysql->connect();
$mysql->select_db();

$other = new other($mysql);

$other->do_some_magic();

 


Any comments, links, book titles that could help me, are more than welcome!


Top
 Profile  
 
PostPosted: Wed Aug 22, 2012 9:42 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Sun Aug 26, 2012 3:54 pm 
Offline
Forum Newbie

Joined: Sat Mar 03, 2012 6:43 am
Posts: 19
You are doing great for beginning with OOP in PHP. There few things from what you wrote that I believe that will make your life easier. First of all you must have naming conventions, even if you don’t like anyone else conventions you should create yours. For me I chose to fallow my logic which happened to be close to ZEND naming conventions e.g. class Model_User_Object mean that is in the path Model/User/Object.php . Also any abstract class ends with the word Abstract and the same with interfaces. Having said that I will go to my second mark, PHP has a very good feature, autoload ( or spl_autoload_register these days ). If you stick to some naming conventions that make sense then you haven’t to use require or include again. Finally from what you wrote , PHP has a great database OOP interface , PDO . Of course probably you need to create your own DB object out of it , in order to extend it or make some things easier for you, so your DB object could just extend PDO.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group