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
dimxasnewfrozen
Forum Commoner
Posts: 84 Joined: Fri Oct 30, 2009 1:21 pm
Post
by dimxasnewfrozen » Mon Nov 30, 2009 3:28 pm
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.
daedalus__
DevNet Resident
Posts: 1925 Joined: Thu Feb 09, 2006 4:52 pm
Post
by daedalus__ » Mon Nov 30, 2009 3:43 pm
can't initialize a class as an object without parameters
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Mon Nov 30, 2009 3:49 pm
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.
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Nov 30, 2009 3:52 pm
daedalus__ wrote: can't initialize a class as an object without parameters
yeah you can.
Yeah, you can't have code within class definitions, only its properties and methods.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Nov 30, 2009 4:23 pm
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
Post
by dimxasnewfrozen » Thu Dec 03, 2009 9:03 am
arborint: I like this, but I'm still getting an error saying:
Fatal error: Call to a member function on a non-object in
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Dec 03, 2009 1:57 pm
I guessed/changed/added some methods to your DbConnector class to demonstrate the idea. I don't know if those methods actually exist.
(#10850)