Reputation: 65
still getting used to PHP classes so a bit of "help /guidance" please
I have a class like this:
class ansa_accounturl_query {
function __construct() {
global $DBH;
global $limit;
$STH = $DBH->query("SELECT frm_url.frm_urlID,frm_url.frm_url FROM frm_url WHERE frm_url.accountID='SOMETHING' ".$limit." ");
$STH->setFetchMode(PDO::FETCH_OBJ);
$this->noforms = $STH->rowCount();
while($row = $STH->fetch()):
$this->frm_urlID[] = $row->frm_urlID;
$this->frm_url[] = $row->frm_url;
endwhile;
}
}
The limit comes from a PHP function - and works.
What I would really like to do is create extend classes for the above example which gives say $this-frm_url
as a function. so that on the "page" I call the class $classcall = new class();
but rather than have to go echo $classcall->frm_url[$key];
I can just call a function like this echo frm_url();
So in the example above there would be 2 extend classes one for frm_urlID and one for frm_url.
Also, in the "master class" am I right in in setting as array? i.e. $this->frm_url[]
as without that I cannot seem to run a loop but the loop does seem "over" complex if you do it this way as you (well I) have to get a count of the array items then run the loop so you (again I) have a for()
statement then a foreach()
. Seems longwinded to me.
Thanks in adavance.
Upvotes: 0
Views: 126
Reputation: 61
<?php
class ansa_accounturl_query {
private static $FIND_ALL_QUERY = "SELECT frm_url.frm_urlID,frm_url.frm_url FROM frm_url WHERE frm_url.accountID='SOMETHING' %s";
private $dbh;
public function __construct( $dbh ) {
$this->dbh = $dbh;
}
public function findUrls() {
$query = $this->dbh->query(vsprintf($queryString, array( func_get_args() ) ) );
$query->setFetchMode(PDO::FETCH_OBJ);
$result = array();
$result["count"] = $query->rowCount();
$result["records"] = array();
while( null !== ( $row = $query->fetch() ) ) {
$result["records"][] = array(
"id" => $row->frm_urlID,
"url" => $row->rm_url
);
}
return $result;
}
}
$ansaQuery = new ansa_accounturl_query($DBH);
$result = $ansaQuery->findUrls();
foreach ( $result['records'] as $row ) {
print sprintf("ID: %d; URL: %s", $row['id'], $row['url']);
}
print sprintf("URLs count: " . $result['count'] );
Upvotes: 0
Reputation: 2900
First: Please do not use globals. If you have to use "global" there is a 90% percent chance that your design is bad. Either pass $DBH and $limit as parameters to __construct($dbh,$limit) or define them as static propertys of ansa_accounturl_query. If you define them as static propertys the values will still be identical for all instance of your class.
Second: If you want to call a method without creating a instance first you can declare the methods static, too. Then you can call them like this:
classname::myMethod(parameter);
if you allways use the same db and the same setting I would suggest you create a class with static propertys and 3 static methods.
Upvotes: 2
Reputation: 50832
What I would really like to do is create extend classes for the above example which gives say $this-frm_url as a function. so that on the "page" I call the class $classcall = new class(); but rather than have to go echo $classcall->frm_url[$key]; I can just call a function like this echo frm_url(); So in the example above there would be 2 extend classes one for frm_urlID and one for frm_url.
echo frm_url();
won't work. You have to use $classcall->frm_url[$key];
unless you define a function like
function frm_url($key){
if (!$key) $key = 0;
$classcall = new ansa_accounturl_query();
return $classcall->frm_url[$key];
}
Upvotes: 0