Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

oo development in php

  • 09-07-2008 12:34pm
    #1
    Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭


    I'm redeveloping one of my sites to take advantage of classes, etc. I've got a class called say 'boards'. It requires about half a dozen parameters. It also needs to use another class, called say 'database'. At the moment I have the following code:[php]class boards {
    var $par1;
    var $par2;
    var $par3;
    var $par4;
    var $par5;
    var $par6;

    function setboards() {
    global $database;

    ...
    }
    }
    $database = new database();
    ...
    $boards = new boards();
    $boards->par1 = "whatever";
    $boards->par2 = "whatever";
    $boards->par3 = "whatever";
    $boards->par4 = "whatever";
    $boards->par5 = "whatever";
    $boards->par6 = "whatever";
    $boards->setboards();[/php]This strikes me as "inefficient" programming, that I'm not using classes in quite the correct way. Should I be passing in the $database class as a parameter to setboards? Or should I set another parameter in the class = $database? Should I be using arrays for the class parameters? Should the boards class be an extension of database?


Comments

  • Registered Users, Registered Users 2 Posts: 4,792 ✭✭✭cython


    corblimey wrote: »
    I'm redeveloping one of my sites to take advantage of classes, etc. I've got a class called say 'boards'. It requires about half a dozen parameters. It also needs to use another class, called say 'database'. At the moment I have the following code:
    class boards {
      var $par1;
      var $par2;
      var $par3;
      var $par4;
      var $par5;
      var $par6;
    
      function setboards() {
        global $database;
    
        ...
      }
    }
    $database = new database();
    ...
    $boards = new boards();
    $boards->par1 = "whatever";
    $boards->par2 = "whatever";
    $boards->par3 = "whatever";
    $boards->par4 = "whatever";
    $boards->par5 = "whatever";
    $boards->par6 = "whatever";
    $boards->setboards();
    
    This strikes me as "inefficient" programming, that I'm not using classes in quite the correct way. Should I be passing in the $database class as a parameter to setboards? Or should I set another parameter in the class = $database? Should I be using arrays for the class parameters? Should the boards class be an extension of database?


    What strikes me as being most inefficient is the apparent lack of a constructor for the class boards. If you had a proper constructor within your class, you could write the equivalent of what you have there in one line, as follows:
    [php]class boards {
    var $par1;
    var $par2;
    var $par3;
    var $par4;
    var $par5;
    var $par6;

    var $database;
    /* I do not list all parameters here for the sake of brevity and my fingers! */
    function boards($par1_in, $par2_in, $par3_in,....., $database_in) {
    $par1 = $par1_in;
    $par2 = $par2_in;
    $par3 = $par3_in;
    // Continue as above for each parameter
    $database = $database_in
    }


    function setboards() {
    //global $database; No longer needed, assuming object constructed with $database passed in

    ...
    }
    }
    $database = new database();
    ...
    $boards = new boards("whatever, "whatever", "whatever", ......, $database_being_passed_in);
    [/php]

    This may not look much more efficient here, but if you want to create initialise objects of class boards in multiple places, then it will be. Constructors are a very important part of objects, and can save some hassle and time.

    The optionod storing a database or passing one in depends on whether an object of the boards class will always be using the same one. If it is likely to be different each time/most of the times it is used, then you should pass it in to set_boards, otherwise you should probably do it as above, whereby it is set once, and available then to all methods in the boards class

    Now I'll be the first to admit that my use of objects in PHP is not good, but this is probably the way that I would approach any use of objects in any language


  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    cython wrote: »
    What strikes me as being most inefficient is the apparent lack of a constructor for the class boards. If you had a proper constructor within your class, you could write the equivalent of what you have there in one line...

    I did consider the constructor, but they're not automatically called for php4 and my host server is still php4. On top of that, wouldn't passing the $database class like that and then assigning to a class variable just mean I'd be using $this->database rather than $database? Not much of a different really.


  • Closed Accounts Posts: 81 ✭✭dzy


    I use PDO for my data access. I define a base data access class that holds a connection to the database. I extend this class to deal with different entities stored in the database.

    Here is a code sample. There is no right way to do this. Its a matter of preference. This happens to be my preference.
    <?php
    
    class Dao
    {
        protected $connection;
        
        public function __construct()
        {
            $this->connection = new PDO("mysql:host=localhost;dbname=MyDb",
                "MyUser", "MyPassword");
        }
    }
    
    class BoardDao extends Dao
    {
        public function getBoard($boardId)
        {
            $sql = "Select Boards.BoardId, Boards.Name
                From Boards
                Where Boards.BoardId=:boardId";
            
            $stmt = $this->connection->prepare($sql);
            $stmt->bindParam("boardId", $boardId, PDO::PARAM_INT);
            $stmt->execute();
            
            return $stmt;
        }
        
        public function updateBoard($board)
        {
            $sql = "Update Boards Set Name=:name
                Where BoardId=:boardId";
            
            $stmt = $this->connection->prepare($sql);
            $stmt->bindParam("boardId", $board["BoardId"], PDO::PARAM_INT);
            $stmt->bindParam("name", $board["Name"], PDO::PARAM_INT);
            $stmt->execute();
        }
    }
    
    $boardId = 1;
    $boardDao = new BoardDao();
    
    // Fetch the board
    $boardReader = $boardDao->getBoard($boardId);
    $board = $boardReader->fetch(PDO::FETCH_ASSOC);
    
    // Update the board
    $board["Name"] = "New name";
    $boardDao->updateBoard($board);
    
    ?>
    


  • Registered Users, Registered Users 2 Posts: 4,475 ✭✭✭corblimey


    dzy wrote: »
    I use PDO for my data access. I define a base data access class that holds a connection to the database. I extend this class to deal with different entities stored in the database.
    Yeah, I think extending the class only makes sense if the extended class also deals with say the database, like your code sample does. If my boards class is all about display or something like that, then it might be a little silly to extend it such.

    As you say, though it's all about preference, and the fact that my code hasn't been ripped apart (yet) means there's probably not that much wrong with it, so I'll continue in this vein.


  • Closed Accounts Posts: 81 ✭✭dzy


    Yeah. Exactly. Do whatever works best for you and don't let somebody tell you that you have to use objects if your procedural code is working just fine.

    Personally, with my views I tend to not use objects. I prefer to just include a template file that iterates my data and outputs html. My scripts follow a pattern of :
    - Create a data access object
    - Pull out the relevant data into some data object. An associative array works fine for most purposes.
    - Include a template file that iterates the data object and outputs html.

    Its worked fine for me so far.


  • Advertisement
Advertisement