PHP classes is this right - php

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.

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.

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];
}

<?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'] );

Related

PHP cascading (passing) subsequent returns through 3 functions

I have been through S/O and have not found this specific problem although my inexperience may mean I've searched by the wrong terms. Any help would be appreciated. Thank you.
Basically, I have three PHP functions (within a class named KeyPositions). The first is called by passing an argument; the second function is called by the first and then produces an array that is passed on to the third function. By using echo and print_r I can see that everything is in place as it is being passed. The problem is occurring on the return from the final function (the 3rd). While my foreach/echo test (just before the return) shows every element in the array is present, that is not what I'm getting once I look at what is returned to the original call. Here are the steps:
Originating call: $keypositions = KeyPositions::getKPSponsor($session_userid);
Second return (where the problem starts?): return self::getKPList($sponsor_list);
Third function: public static function getKPList($arr)
Test before return (shows array contents perfectly): foreach($kppositions as $s) { echo $s['kp_root'],"<br>"; }
Return statement: return $kppositions;
But when I go back to the original function call and run this test I only receive a subset of what I would've expected from the return (above): foreach($keypositions as $t) { echo $t['id'],"<br>"; }
Is there something that I'm missing when an array is passed from one function to the next? All arrays are associative. I intentionally did not include the lengthy code within each of the functions because the functions themselves seem to be performing perfectly based on the test I do just before each return (meaning what I expect to be inside the array is fully intact at each step). I believe this may have something to do with the passing of returns from one function to the next? Any direction here would be appreciated. Thank you.
Okay. Sorry. My thought was to not muddy things with all the code. Here it is. Keep in mind, this is for testing and not for final (e.g., no PHP injection work yet.)
public static function getKPSponsor($userid)
{
return self::getKPSponsors('positions.jobholder_uid = "' .$userid. '"');
}
public static function getKPSponsors($criteria = '')
{
$sponsor_levels = array();
$sponsor_list = array();
$criteria = " WHERE $criteria";
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query_sponsors = "SELECT positions.position_id AS id, jobholder_uid AS uid FROM positions $criteria";
$result_sponsors = $mysqli->query($sql_query_sponsors);
while ($sponsor_level = mysqli_fetch_assoc($result_sponsors)){
$sponsor_id = $sponsor_level['uid'];
$sponsor_level['items'] = self::getKPSponsors('positions.positionmgr_uid = "' .$sponsor_id. '"');
$sponsor_levels[] = $sponsor_level;
}
foreach($sponsor_levels as $r) {
$sponsor_list[] = $r['id'];
}
mysqli_free_result($result_sponsors);
unset($r);
return self::getKPList($sponsor_list);
}
public static function getKPList($arr)
{
$kppositions = array();
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query_keypositions = "SELECT key_positions.kp_id AS id, key_positions.kp_root, key_positions.kp_area, key_positions.c_ksa, key_positions.c_relationship, key_positions.c_rare, key_positions.urgency, positions.jobholder_uid, master.last_name, CONCAT(master.first_name,' ',master.last_name) AS full_name FROM key_positions INNER JOIN positions ON positions.position_id = key_positions.kp_sponsor INNER JOIN master ON positions.jobholder_uid = master.email_address WHERE key_positions.kp_sponsor IN (" . implode(",", $arr) . ");";
$result = $mysqli->query($sql_query_keypositions);
while ($kp3 = mysqli_fetch_assoc($result)) {
$kppositions[] = $kp3;
}
foreach($kppositions as $s) { // My testing ...
echo $s['kp_root'],"<br>";
}
mysqli_free_result($result);
return $kppositions;
}
In case this helps further, the output from my test BEFORE the return yields:
1000014
1000015
1000016
1000012
1000006
1000022
1000017
1000018
1000019
1000020
1000021
1000008
1000009
1000010
1000011
1000004
1000005
1000007
1000013
1000003
1000001
1000002
The output AFTER the return yields:
1000001
1000002

PHP - What's the best way to handle empty function/method arguments?

I have the function/method below inside a class that I'm creating and I'm just wondering what's the best way to handle empty/null arguments.
For example, in the following example, if I wanted to just set just the category when calling the function, I would need to use:
$data = $class->get_top_headlines( null, 'technology' );
Is there any way of calling the function more efficiently? I know I could pass the arguments as an array instead, but just wondered if there's any way of doing something like:
$data = $class->get_top_headlines( $category='technology' ); and automatically leaving the other arguments as their default of null?
public function get_top_headlines( $query=null, $category=null, $country=null, $sources=null, $page_size=null, $page=null ){
$url = $this->api_url . $this->endpoint_top_headlines;
$params = array();
if ( $query !== null ){
$params['q'] = urlencode( $query );
}
if ( $category !== null ){
$params['category'] = $category;
}
if ( $country !== null ){
$params['country'] = $country;
}
if ( $sources !== null ){
$params['sources'] = $sources;
}
if ( $page_size !== null ){
$params['pageSize'] = $page_size;
}
if ( $page !== null ){
$params['page'] = $page;
}
$params['apiKey'] = $this->api_key;
$url_query = http_build_query( $params );
$url = $url . '?' . $url_query;
echo $url;
$obj = $this->get_response( $url );
return $obj;
}
Try passing an array, and then using an array_merge
$data = $class->get_top_headlines(['category' => 'technology']);
Then in your function, have an array of defaults, then do your merge.
$settings = array_merge($settings, $passedInArray);
http://php.net/manual/en/function.array-merge.php
I think
(null, 'technology' );
might be less actual coding but a different solution might be to use OOP. You said it's already a method of a class so you could do something like:
$obj = new thatClass;
$obj->technology = $technology;
$obj->get_top_headlines();
in the Class:
Class thatClass{
$technology = null;
$category = null;
$query = null;
//...
public function get_top_headlines(){
if ( $this->query !== null ){
$params['q'] = urlencode( $this->query );
}
if ( $this->category !== null ){
$params['category'] = $this->category;
}
if ( $this->technology !== null ){
$params['technology'] = $this->technology;
}
//method code..
}
//class code..
}
The problem with this approach is if you need to call that same function again passing a different parameter in the same class instance, depending on your application you might need to manually set back to null the previous parameter (now an object attribute)
I would solve this problem by creating a new class or data structure which will encapsulate all the logic of validating and generating the URL and then use it everywhere I need it.
Here's a sample class.
class HeadLineParameters
{
private $params = [];
public function setQuery($query)
{
// validate/transform query data
$this->params['q'] = urlencode($query);
return $this;
}
public function setCategory($category)
{
// validate/transform category data
$this->params['category'] = $category;
return $this;
}
public function generateUrl()
{
return http_build_query( $this->params );
}
}
$params = new HeadLineParameters;
$params->setQuery($query)
->setCategory($category);
You just pass one argument and you know that it's just an instance of HeadLineParameters.
$class->get_top_headlines($params);
This solution doesn't pollute your current class with unnecessary state or fields. It is easy to test, and it has only one job. You can extend it easily, you can set default values, you can also validate it as you like.
Edit: Why you shouldn't add more fields to your current class?
If you add more fields to your current class you'll be breaking the single responsibility principle, and any method of this class can change these fields too. It shouldn't be a problem if these fields really belong there and more methods require them. This is fine if you are using OOP.
I am not sure what other people think about passing associated arrays to functions, but they are hard to handle if you have no documentation available. I have had trouble with them when reading some external code, and most of time I wasn't sure what's the data I was dealing with.

PHP : object method+proprety call from var

I want to call an object method+property with the content stored in a var ...
for example :
// setup the object
$xpath = new DOMXpath();
// setup the 'method'+'property' to call
$var1 = "query('something')->item(O)->nodeValue";
$return = $xpath->$var1();
Obviously, I make a mistake ... assuming that direct call is working, i.e.:
$return2 = $xpath->query('something')->item(0)->value;
echo "Return2 : ".$return2; //print okeedokee ...
How to pass args to query()? And how to add extra args to it?
I think you have to call
$return=$xpath->$var1;
Note : call_user_func is the function you need
Example :
alpha.php
class Alpha
{
public function getAlpha($arr_input)
{
echo "<pre>";
print_r($arr_input);
}
}
index.php
include_once 'alpha.php';
$post = array('one','two','three');
$obj_alpha = new Alpha();
call_user_func( array( $obj_alpha , 'getAlpha' ), $post ) ;
//here I call `getAlpha` function from object of class alpha (`$obj_alpha`)
with argument `$post`
//will print $post array
You can do this using eval():
$return = eval('return $xpath->' . $var1 .';');
However, using eval() with user-input is pretty much always a bad idea. So be careful there.

Dynamically call Class with variable number of parameters in the constructor

I know that it is possible to call a function with a variable number of parameters with call_user_func_array() found here -> http://php.net/manual/en/function.call-user-func-array.php . What I want to do is nearly identical, but instead of a function, I want to call a PHP class with a variable number of parameters in it's constructor.
It would work something like the below, but I won't know the number of parameters, so I won't know how to instantiate the class.
<?php
//The class name will be pulled dynamically from another source
$myClass = '\Some\Dynamically\Generated\Class';
//The parameters will also be pulled from another source, for simplicity I
//have used two parameters. There could be 0, 1, 2, N, ... parameters
$myParameters = array ('dynamicparam1', 'dynamicparam2');
//The instantiated class needs to be called with 0, 1, 2, N, ... parameters
//not just two parameters.
$myClassInstance = new $myClass($myParameters[0], $myParameters[1]);
You can do the following using ReflectionClass
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = array ('dynamicparam1', 'dynamicparam2');
$reflection = new \ReflectionClass($myClass);
$myClassInstance = $reflection->newInstanceArgs($myParameters);
PHP manual: http://www.php.net/manual/en/reflectionclass.newinstanceargs.php
Edit:
In php 5.6 you can achieve this with Argument unpacking.
$myClass = '\Some\Dynamically\Generated\a';
$myParameters = ['dynamicparam1', 'dynamicparam2'];
$myClassInstance = new $myClass(...$myParameters);
I implement this approach a lot when function args are > 2, rather then end up with an Christmas list of arguments which must be in a specific order, I simply pass in an associative array. By passing in an associative array, I can check for necessary and optional args and handle missing values as needed. Something like:
class MyClass
{
protected $requiredArg1;
protected $optionalArg1;
public function __construct(array $options = array())
{
// Check for a necessary arg
if (!isset($options['requiredArg1'])) {
throw new Exception('Missing requiredArg1');
}
// Now I can just localize
$requiredArg1 = $options['requiredArg1'];
$optionalArg1 = (isset($options['optionalArg1'])) ? $options['optionalArg1'] : null;
// Now that you have localized args, do what you want
$this->requiredArg1 = $requiredArg1;
$this->optionalArg1 = $optionalArg1;
}
}
// Example call
$class = 'MyClass';
$array = array('requiredArg1' => 'Foo!', 'optionalArg1' => 'Bar!');
$instance = new $class($array);
var_dump($instance->getRequiredArg1());
var_dump($instance->getOptionalArg1());
I highly recommend using an associative array, however it is possible to use a 0-index array. You will have to be extremely careful when constructing the array and account for indices that have meaning, otherwise you will pass in an array with offset args and wreck havoc with your function.
You can do that using func_get_args().
class my_class {
function __construct( $first = NULL ) {
$params = func_get_args();
if( is_array( $first ) )
$params = $first;
// the $params array will contain the
// arguments passed to the child function
foreach( $params as $p )
echo "Param: $p\n";
}
}
function my_function() {
$instance = new my_class( func_get_args() );
}
echo "you can still create my_class instances like normal:";
$instance = new my_class( "one", "two", "three" );
echo "\n\n\n";
echo "but also through my_function:";
my_function( "one", "two", "three" );
Basically, you simply pass the result of func_get_args to the constructor of your class, and let it decide whether it is being called with an array of arguments from that function, or whether it is being called normally.
This code outputs
you can still create my_class instances like normal:
Param: one
Param: two
Param: three
but also through my_function:
Param: one
Param: two
Param: three
Hope that helps.
I've found here
Is there a call_user_func() equivalent to create a new class instance?
the example:
function createInstance($className, array $arguments = array())
{
if(class_exists($className)) {
return call_user_func_array(array(
new ReflectionClass($className), 'newInstance'),
$arguments);
}
return false;
}
But can somebody tell me if there is an example for classes with protected constructors?

Static method, Zend_View error

I have a static method 'findAll' on a model which basically gets all rows with certain criteria. This method works fine and I can call it using:
$m::findAll();
Where $m is the model name as a variable. I can output this and it returns correct results. However, when assigning this to a variable in the Zend_View object, as:
$this->view->viewvariable = $m::findAll();
I get the error:
Zend_Db_Table_Exception: Too many
columns for the primary key
Any ideas why?
Find all function:
final public static function findAll($where = false, array $options = array()) {
$object = new static();
if (!empty($options)) $options = array_merge($object->options, $options);
else $options = $object->options;
$run = $object->buildDefaultSelect($where, $options);
$rows = $run->fetchAll();
if ($options['asObject'] == true) {
$result = array();
foreach ($rows as $r) {
$class = new static();
$class->setInfo($r);
$result[] = $class;
}
return $result;
} else {
if (count($rows) > 0) return $rows;
else return array();
}
}
Note: This function works fine everywhere apart from when assigning to a view variable. If I run the below (not assigning it to a view variable), it shows the correct array data.
var_dump($m::findAll($module['where'], $module['options']));
exit;
In my view (I have replaced the actual name with viewvariable for the sake of this post):
<?php foreach($this->viewvariable as $item) { ?>
//Do some echoing of data in $item
//Close foreach
I doubt the issue is with Zend_View. It's hard to tell without seeing your code, but my guess is that findAll() is using the Zend_Table_Db find() function incorrectly.
To my knowledge, the only place that throws that exception is the find() function on Zend_Db_Table_Abstract.
Perhaps, inside the findAll() function (or in a function it calls) you're doing one of these:
$zendDbTable->find(1,2) //is looking for a compound key
$zendDbTable->find(array(1,2)) //is looking for two rows
When you really want the opposite.

Categories