CodeIgniter access model with $$ fails - php

I have a model called Treatment in CodeIgniter. I want to load and use this model 'dynamically'. That is, I don't want to have to call it directly by name (I am trying to generalize some code to use whatever model I tell it).
So, I do this:
$namespace = 'blah';
$modelName = 'Treatment';
...
$this->load->model($namespace . '/' . $modelName);
$this->model = $this->$$modelName;
However, I get an error when accessing the $this->$$modelName variable, saying that the variable 'Treatment' is undefined:
Undefined variable: Treatment ... Fatal error: Cannot access empty
property in /mydir/application/controllers/rest/base_rest.php on line
202.
Where line 202 is the line where I am using the $this->$$modelName variable.
Now, if I changed line 202 to be:
$this->model = $this->Treatment;
It works fine.
Does anyone know why I can't seem to use the PHP $$ syntax here?

You can't because it's not a supported syntax. Try
$this->{$modelName}
instead. e.g.
php > class foo { public $bar = 42; }
php > $x = new foo();
php > $y = 'bar';
php > echo $x->$$y;
PHP Notice: Undefined variable: bar in php shell code on line 1
PHP Fatal error: Cannot access empty property in php shell code on line 1
php > echo $x->{$y};
42php >

Related

How can I use the compile time constant __LINE__ in a string [PHP]

I have a PHP class file, I need to use the __ CLASS __ , __ LINE __ constants in lots of place. I don't want to write these tow constants in every place so, I have create the method called log_suffix() in my class file and using that method.
function detail(){
$leave_groups = LeaveGroup::get_leave_group_details($this->company_id, 1, true);
if($this->debug){
print_r($leave_groups->toArray());
}
if($leave_groups->count() === 0){
Log::error("log: " . $this->log_suffix() );// Line 39
return false;
}
return $leave_groups;
}
function log_suffix(){
return "$this->company_code | $this->company_name \t on File: ". _CLASS_. "\t". __LINE__ ; // Line no 51
}
the problem is, It is printing the line no 51 ( line constant written line ).
Is it possible to get the compile time line no that is 39 according to the shared image.
I know that with a try catch exception we can do this ( get the file and line no ).
rather than that is there any other way to sort out this issue.
here is online editor link
As per magic constants documentation, __LINE__ contains the "The current line number of the file.", that is the line where it is used. Therefore, looking at the example posted, in the log_suffix() function it will always print 51, wherever this function is called.
You could pass __LINE__ as a parameter of this function when it is called, , because it will hold the line number where the function is actually called, but this is what you want to avoid.
Another possibility is to use debug_backtrace() function in log_suffix(), with parameter limit = 1. It will return an array with one key, whose value is an array. The 'line' key holds the line of the previous calling scope.
A quick example:
$debugTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1);
$callerLine = $debugTrace[0]['line'];

How exactly is this function a call-by-reference function?

I get this warning message:
PHP Notice: Only variables should be assigned by reference in /home/sophia/gitter/project/rational-office-2019/tmp/zog.php on line 16
What is in line this 'zog.php' file?
$the_mainrdn = &obtaino($zogcn,'mainrdn');
So, you would think from this warning that the obtaino function would have a second argument that is supposed to be called by reference - but here is the start and end of the function in question:
function obtaino ( $the_xcont, $the_clnom )
{
$awesome_dom = new DOMDocument();
$awesome_dom->loadHTML($the_xcont);
-- <some more stuff here> --
return $awesome_bdv;
}
as you can see, there is no & symbol before the $the_clnom at the opening of the function - so I do not see how this is call by reference.
So what am I missing?

PHP error on testing server

I have a bit of PHP code that works fine on my production server but not on my test server. Here is the code:
function callProcedure0(&$connection, $procname, $dofunction)
{
mysqli_multi_query($connection, "CALL " . $procname . "();");
$first_result = 1;
do
{
$query_result = mysqli_store_result($connection);
if ($first_result)
{
$dofunction($query_result);
$first_result = 0;
}
if ($query_result)
{
mysqli_free_result($query_result);
}
$query_result = NULL;
} while(mysqli_next_result($connection));
}
...
function doGenres($in_result)
{
global $genre_array, $game_array, $genre_order_array;
$genre_count = 1;
// foreach is necessary when retrieving values since gaps may appear in the primary key
while ($genre_row = mysqli_fetch_row($in_result)) // line 81 is here!
{
$genre_array[] = $genre_row[0];
$genre_order_array[$genre_row[1] - 1] = $genre_count;
$game_array[] = [[],[]];
$genre_count += 1;
}
}
...
callProcedure0($con, "get_genres_front", doGenres); // line 138 is here!
The "get_genres_front" bit refers to a stored procedure on my database. Here are the errors:
Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138
Again, there are no problems on the production server which is using Apache 2.2.23, MySQL 5.1.73-cll, PHP 5.4.26. The test server where things are broken is running Apache 2.4.10, MySQL 5.6.21, PHP 5.5.19.
Did something change in recent software versions? Thanks.
[edit]
This is not a duplicate question. I'm worried about the first error. I already know what to do about the second error which I have deleted.
The code you have posted is wrong, you must pass function name as string and then use call_user_func to invoke this function.
In your callProcedure0 function change
$dofunction($query_result);
to
call_user_func($dofunction, $query_result);
And then call it with the function name as string like this
callProcedure0($con, "get_genres_front", "doGenres");
The above code could work also with invoking the function with
$dofunction($query_result);
on some php versions, but the line where you pass the function name it should be string, otherwise PHP assumes it is a constant.

FACEBOOK API CALL - PHP SDK FATAL ERROR

trying to get every post and comment from a facebook page, I made this function that should go through the pagination:
$req = $facebook->api("/" . $pagename . "/?fields=posts.fields(message,link,created_time,shares,likes,comments)");
function parcours_arbre($ab)
{
GLOBAL $facebook;
GLOBAL $pagename;
$next = create_request($ab['posts']['paging']['next']);
$next_req = $facebook->api($pagename.$next);
$ab_next = $next_req['data'];
$prev = create_request($ab['posts']['paging']['previous']);
$prev_req = $facebook->api($prev);
$ab_prev = $prev_req['data'];
if (empty($ab)) {
display_results($ab['posts']['data']);
} else {
parcours_arbre($ab_next);
parcours_arbre($ab_prev);
}
}
I unfortunately get the following error:
Notice: Undefined index: posts in /form.php on line 36
Notice: Undefined offset: 3 in /utils.php on line 20
Notice: Undefined offset: 4 in /utils.php on line 20
Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /sdk/src/base_facebook.php on line 1271
Any idea how i could avoid it or what is going on? Would this go away if i use the "until" statement in my api request?
Thanks a lot,
To explain each error
the variable $ab which is an argument to the function, does not have a "posts" index. You should try to var_dump this variable so you can see what it actually looks like.
same as above
same as above
the api function takes 3. The path which should just be #pagename. The method ("GET" or "POST") most likely POST because GET is causing an error. The parameters, which should be array("fields" => "posts.fields(message,link,created_time,shares,likes,comments)")
I noticed that for next you have the code
$next_req = $facebook->api($pagename.$next);
but for previous you have
$prev_req = $facebook->api($prev);
Might want to look into this.

PHP Class Variable Access

Seems like a simple enough question, so my apologies for asking. As a precursor I'm not necessarily 'new', but rather not so well-versed in PHP.
I have a class, declared as follows:
class User
{
public $id = "";
public function User()
{
$this->$id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;
}
}
Which seems simple enough, however - upon construction, I get the following set of errors:
Notice: Undefined variable: id in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Fatal error: Cannot access empty property in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Sorry for asking something so simple. The line in question starts with "$this->$id".
Remove the $ symbol at the 'id' place:
$this->id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;

Categories