I believe my class is correct but when I try to echo the output of the class I get an error on line 28: the line " echo 'Your full name ...." is line 28. Any help would be nice
<?php
echo 'Your full name is ' . $person->retrieve_full_name() . '.';
?>
This is where I created the function "retrieve_full_name"
public function __retrieve_full_name() {
$fullname = $this->firstname . ' . ' . $this->lastname;
return $fullname;
}/* This ends the Full Name Function*/
the error I get is
Fatal error: Call to undefined method stdClass::retrieve_full_name() in /home/mjcrawle/processlogin2.php on line 28
your function is called __retrieve_full_name, but you call retrieve_full_name. notice the missing underscores.
double underscores are usually the prefix for php internal/magic functions, i would advise against using them in your function names.
Your immediate error is due to the fact that you call your method by the wrong name. And:
don't use underscores to start a method name
don't use underscores in method names at all, if you care for best practice, use camel casing instead retrieveFullName().
public function retrieve_full_name() {
Related
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'];
<?php
require_once('includes/auth.php');
function getMarkets(){
$count_markets=querydb("SELECT count(*) as marketcount from markets ");
$result=$count_markets('marketcount');
return $result;
}
I have the above code and i am getting the error below:
Fatal error: Function name must be a string
Seen the Error was using $count_markets('marketcount'); instead of $count_markets['marketcount'];
$result=$count_markets('marketcount'); - remove $ from count_markets.
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){
.
.
.
}
The idea here is to create a method for logging and debugging purposes, that doesn't require passing said method the associated 'magic constants'.
Effectively, I'm trying to achieve this using a method definition like so:
function Debug($Message,$File=__FILE__,$Line=__LINE__)
{
...
}
The problem I am running in to is that if I define the above method in a file other than the one I am 'debugging', I end up with the file and line from the file the method is defined in, rather than the one I am 'debugging'.
Consider the following set of files:
Debugging.php
<?
function Debug($Message,$File=__FILE__,$Line=__LINE__)
{
echo("$File ( $Line ) :: $Message");
}
?>
Testing.php
<?
Debug("Some message");
?>
Output:
Debugging.php ( 1 ) :: Some message
When the invocation of the message occurred in the second file - which, as should be clear by this point, isn't the intended implementation. I could of course pass the 'Debug' method those magic constants at the time of invocation, but I'm looking to eliminate unnecessary code if possible.
You would use the function debug_backtrace like so.
function Debug($Message)
{
$backtrace = debug_backtrace();
echo($backtrace[0]['file'] .'(' . $backtrace[0]['line'] . ') :: ' . $Message);
}
Example:
const USERNAME_MIN_LENGTH = '2';
private $uesrname_error_message= 'ERROR: Max. username length is ' . USERNAME_MIN_LENGTH;
I get this error with the code provided above:
Parse error: syntax error, unexpected '.', expecting ',' or ';'
The error occurs for the line in which the $test variable is defined.
I'm using PHP 5.5.12 version.
Why?
Expressions in property declarations are not (yet) supported by PHP. Some expressions will be allowed from PHP 5.6 onwards. You can read more about it on PHP's wiki.
Your specific example should work in PHP 5.6.
You need to define variable $test inside the constructor like this:
class yourClass{
private $test;
function __construct(){
const USERNAME_MIN_LENGTH = '2';
$this->test = 'Max. username length is ' . USERNAME_MIN_LENGTH;
}
}
This only works in PHP 5.6:
private $test = 'Max. username length is ' . USERNAME_MIN_LENGTH;