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

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?

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

Catchable fatal error: Argument 1 passed to CorenlpAdapter::getOutput()

I get the following error:
Catchable fatal error: Argument 1 passed to CorenlpAdapter::getOutput() must be an instance of string, string given, called in /Library/WebServer/Documents/website/php-stanford-corenlp-adapter/index.php on line 22 and defined in /Library/WebServer/Documents/website/php-stanford-corenlp-adapter/src/CoreNLP/CorenlpAdapter.php on line 95
index.php 21 and 22 contain:
$text1 = 'I will meet Mary in New York at 10pm';
$coreNLP->getOutput($text1);
corenlpAdapter.php lines 95 and onwards contain:
public function getOutput(string $text){
if(ONLINE_API){
// run the text through the public API
$this->getServerOutputOnline($text);
} else{
// run the text through Java CoreNLP
$this->getServerOutput($text);
}
// cache result
$this->serverMemory[] = $this->serverOutput;
if(empty($this->serverOutput)){
echo '** ERROR: No output from the CoreNLP Server **<br />
- Check if the CoreNLP server is running. Start the CoreNLP server if necessary<br />
- Check if the port you are using (probably port 9000) is not blocked by another program<br />';
die;
}
/**
* create trees
*/
$sentences = $this->serverOutput['sentences'];
foreach($this->serverOutput['sentences'] as $sentence){
$tree = $this->getTreeWithTokens($sentence); // gets one tree
$this->trees[] = $tree; // collect all trees
}
/**
* add OpenIE data
*/
$this->addOpenIE();
// to get the trees just call $coreNLP->trees in the main program
return;
}
Why exactly am I getting this error when text1 is a string?
I am the original author of this class. As you can see, the function getOutput looks like this:
public function getOutput(string $text){
...
}
Change that to:
public function getOutput($text){
...
}
The function tries to enforce that the input is string. The original code should work. However, it seems that in your case, PHP thinks "string" is not actually a string. Maybe the coding environment (the IDE) you are using uses the wrong character set? Or maybe you copy-pasted the code from HTML into the IDE or something like that. Thus whilst it says "string" on the screen, it's not actually a string to PHP.
If you are sure the input is a string, you can safely change the code like above. The class should then work normally.
public function getOutput($text){
.
.
.
}

CodeIgniter access model with $$ fails

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 >

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;

Get filename and line number of start of function declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out where a function is defined?
included php file to know it's file name?
Can I get the filename and line number of the start of a function declaration in PHP?
Say, I have the following code:
1. /**
2. * This function shows the foo bar.
3. */
4. function foo_bar($subject) {
5. echo "Foo bar:\n";
6. if ($subject == "none") {
7. trigger_error("Wrong argument given to 'foo_bar()' in ... on line ....", E_USER_WARNING);
8. }
9. ...
10. }
When I call foo_bar("none"), the function must throw an error like this:
Warning: Wrong argument given to 'foo_bar()' in /home/web/mcemperor on line 4.
Can I retrieve the filename and the line number of start of the function declaration?
You're looking for ReflectionFunction.
$r = new ReflectionFunction('foo_bar');
$file = $r->getFileName();
$startLine = $r->getStartLine();
It's that simple... It works for any defined function, whatever one that you passed in to the constructor argument.
The function name could be found with PHP exceptions, which are more interesting for error triggering : http://www.php.net/manual/en/exception.gettrace.php
$backtrace = debug_backtrace();
$line = $backtrace[0]['line'];
$file = $backtrace[0]['file'];
http://www.php.net/manual/en/function.debug-backtrace.php

Categories