OOP Extending Class and new objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

OOP Extending Class and new objects

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: OOP Extending Class and new objects

Post by daedalus__ »

can't initialize a class as an object without parameters

Code: Select all

$blah = new class();
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: OOP Extending Class and new objects

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: OOP Extending Class and new objects

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Extending Class and new objects

Post 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;
    }
}
(#10850)
dimxasnewfrozen
Forum Commoner
Posts: 84
Joined: Fri Oct 30, 2009 1:21 pm

Re: OOP Extending Class and new objects

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP Extending Class and new objects

Post 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.
(#10850)
Post Reply