Page 1 of 1

OOP Extending Class and new objects

Posted: Mon Nov 30, 2009 3:28 pm
by dimxasnewfrozen
I have the following code:

Code: Select all

 
 
require_once 'includes/DbConnector.php';
class Layout{
    
    $connector = new DbConnector;   
    function SubHeader($id){
 
      $result = $connector->query("SELECT * from int_pages where link_id = '$id'");
       echo "<div class=subheader>";
          while ($row = $connector->fetchArray($result)){
         echo "<a href=" . $row['link_name']. ">". $row['link_name'] . "</a>";
         }
         echo "</div>";
    }
}
 
I am getting a parse error at the top line where it's trying to create an object for the DbConnector. I'm not sure how to do this within a class.

Re: OOP Extending Class and new objects

Posted: Mon Nov 30, 2009 3:43 pm
by daedalus__
can't initialize a class as an object without parameters

Code: Select all

$blah = new class();

Re: OOP Extending Class and new objects

Posted: Mon Nov 30, 2009 3:49 pm
by AbraCadaver
It all depends upon what you're going to do with this, but you might assign it in the constructor. Or if you only use it once, then in the function.

Code: Select all

class Layout{
    private $connector;
 
    public __construct() {
        $this->connector = new DbConnector();
    }
}
You might want to rethink your design. Maybe investigate some design patterns and best practices, like bootstrapping your app and placing the dbconnector in a registry.

-Shawn

Re: OOP Extending Class and new objects

Posted: Mon Nov 30, 2009 3:52 pm
by jackpf
daedalus__ wrote:can't initialize a class as an object without parameters

Code: Select all

$blah = new class();
yeah you can.

Yeah, you can't have code within class definitions, only its properties and methods.

Re: OOP Extending Class and new objects

Posted: Mon Nov 30, 2009 4:23 pm
by Christopher
This would be better to do something like this:

Code: Select all

require_once 'includes/DbConnector.php';
 
$model = new PagesModel(new DbConnector());
$layout = new Layout();
echo $layout->SubHeader($model->findPages('foo'));
 
class PagesModel {
    
    function __construct($connector){
        $this->connector = $connector;   
    }
 
    function findPages($id){
 
      $result = $this->connector->query("SELECT * from int_pages where link_id = '$id'");
      $pages = array();
      if (! $result->isError() ) {
          while ($row = $result->fetchArray()){
              $pages[] = $row;
         }
      }
      return $pages;
    }
}
 
class Layout{
    
    function SubHeader($pages){
 
       $str = "<div class=subheader>";
       foreach ($pages as $row){
           $str .= "<a href=" . $row['link_name']. ">". $row['link_name'] . "</a>";
       }
       $str .= "</div>";
       return $str;
    }
}

Re: OOP Extending Class and new objects

Posted: Thu Dec 03, 2009 9:03 am
by dimxasnewfrozen
arborint: I like this, but I'm still getting an error saying:

Fatal error: Call to a member function on a non-object in

Re: OOP Extending Class and new objects

Posted: Thu Dec 03, 2009 1:57 pm
by Christopher
I guessed/changed/added some methods to your DbConnector class to demonstrate the idea. I don't know if those methods actually exist.