how to get through this Fatal Error problem? - php

**Fatal error: Method Template::__toString() must not throw an exception, caught ParseError: syntax error, unexpected end of file in C:\xampp\htdocs\joblister\index.php on line 0**
i dont know where is the wrong part? can you guys help me? i kept trying but nothing can fix it.
<?php include_once 'config/init.php'; ?>
<?php
$job = new Job;
$template = new Template('templates/frontpage.php');
$template->title = 'Latest Jobs';
$template->jobs = $job->getAllJobs();
echo $template;
?>

this is because you are trying to echo an object not string ...
in the template class you either need to implement the magic function __toString() and define how the object should be printed or you may have your custom method in the class for example getHtml() .. which returns the built string and echo it like that
echo $template->getHtml();

Related

PHP: How to catch Fatal error: Cannot declare class [myclassname], because the name is already in use

I am implementing a plugin controller CLASS for client created custom plugins.
Since the names of the classes of each plugin could very well end up clashing with other plugins already installed, I want to make sure we don't get fatal errors when a new plugin comes in.
I want to report to the user that the new plugin is clashing with an already installed one.
So basically atm I testing with 2 files both containing exactly the same code and am getting:
Fatal error: Cannot declare class [myclassname], because the name is already in use
I have tried catching this with no success using:
try {
include_once $file;
} catch (ClosedGeneratorException|DOMException|ErrorException|IntlException|LogicException|BadFunctionCallException|BadMethodCallException|DomainException|InvalidArgumentException|LengthException|OutOfRangeException|PharException|ReflectionException|RuntimeException|OutOfBoundsException|OverflowException|PDOException|RangeException|UnderflowException|UnexpectedValueException|SodiumException $ex) {
echo "Unable to load file.";
}
All these I got from Lists of Throwable and Exception tree as of 7.2.0 in https://www.php.net/manual/en/class.exception.php
The manual specifies that:
The thrown object must be an instance of the Exception class or a
subclass of Exception. Trying to throw an object that is not will
result in a PHP Fatal Error.
Is it possible that this is not an object of instance/suclass of the Exception class?
What am I missing?
Short answer: no, you're not missing anything. Declaring a duplicate class name can't be caught in any version of PHP (including 8.0.0 as far as the alpha releases). See https://3v4l.org/2TLA3
For some additional background, PHP does sometimes move errors like these underneath the Throwable hierarchy, so that they can be detected at runtime. PHP 7 added support for catching an attempt to instantiate a missing class, and 7.3 added support for catching an attempt to extend a missing class. See https://3v4l.org/BDnm9 for a brief demo of those.
In the end, to avoid this fatal error I decide to parse the files for the first available class declaration using tokens as follows:
public function getClassInFile($filenpath) {
$src = $this->uncommentFile($filenpath);
$class ='';
if (preg_match('/class[\s\n]+([a-zA-Z0-9_]+)[\s\na-zA-Z0-9_]+\{/', $src, $matches)) {
$class = $matches[1];
}
return $class;
}
public function uncommentFile($filenpath) {
$fileStr = file_get_contents($filenpath);
$newStr = '';
$commentTokens = array(T_COMMENT);
if (defined('T_DOC_COMMENT'))
$commentTokens[] = T_DOC_COMMENT; // PHP 5
if (defined('T_ML_COMMENT'))
$commentTokens[] = T_ML_COMMENT; // PHP 4
$tokens = token_get_all($fileStr);
foreach ($tokens as $token) {
if (is_array($token)) {
if (in_array($token[0], $commentTokens))
continue;
$token = $token[1];
}
$newStr .= $token;
}
return $newStr;
}
In this way, I can check whether the class I am expecting to find is in there, or if the class in there already exists, thus avoiding to include_once altogether in these cases.
Problem solved.

PHP how to have declare a class member?

Slightly confused with PHP because it does not declare object variable types.
This is simplified code which does not work. I get why I get an error but not sure how in PHP I can specify that $pb is a PushBot object and so it has methods which can be used.
class Bot
{
public $pb;
//Constructor
function __construct(){
require_once('../PushBots.class.php');
// Application ID
$appID = '';
// Application Secret
$appSecret = '';
// Push The notification with parameters
$this ->pb = new PushBots();
$this ->pb->App($appID, $appSecret);
}
//Method. The $this->pb->Push() does not work
public function sendMessage(){
$this->pb->Push();
}
}
//Client calling the class
$bot = new Bot();
$bot->sendMessage();
The error I get is :
Parse error: syntax error, unexpected '$' for when the line
$this->pb->Push();
is called.
I guess its because it does not know that $pb is a PushBot object at this stage ?
Can I not declare it something like :
Public Pushbot $pb;
Parse error: syntax error, unexpected '$' for when the line
This is a parse error, meaning your code has not even been run yet. Check your code for any sytnax errors (the code you posted does not contain the syntax error).
how in PHP I can specify that $pb is a PushBot object
Although unrelated to the syntax error, if you were using some sort of dependency inversion you could use type-hinting to require a certain object to be passed:
// will cause fatal error if anything other than an object of type Pushbot is passed
function __construct(Pushbot $pb)

I'm trying to handle an Exception in PHP but stack error is still showing rather than being handled by catch

I'm calling a method that I know could cause an error and I'm trying to handle the error by wrapping the code in a try/catch statement...
class TestController extends Zend_Controller_Action
{
public function init()
{
// Anything here happens BEFORE the View has rendered
}
public function indexAction()
{
// Anything `echo`ed here is added to the end of the View
$model = new Application_Model_Testing('Mark', 31);
$this->view->sentence = $model->test();
$this->loadDataWhichCouldCauseError();
$this->loadView($model); // this method 'forwards' the Action onto another Controller
}
private function loadDataWhichCouldCauseError()
{
try {
$test = new Application_Model_NonExistent();
} catch (Exception $e) {
echo 'Handle the error';
}
}
private function loadView($model)
{
// Let's pretend we have loads of Models that require different Views
switch (get_class($model)) {
case 'Application_Model_Testing':
// Controller's have a `_forward` method to pass the Action onto another Controller
// The following line forwards to an `indexAction` within the `BlahController`
// It also passes some data onto the `BlahController`
$this->_forward('index', 'blah', null, array('data' => 'some data'));
break;
}
}
}
...but the problem I have is that the error isn't being handled. When viewing the application I get the following error...
( ! ) Fatal error: Class 'Application_Model_NonExistent' not found in /Library/WebServer/Documents/ZendTest/application/controllers/TestController.php on line 23
Can any one explain why this is happening and how I can get it to work?
Thanks
use
if (class_exists('Application_Model_NonExistent')) {
$test = new Application_Model_NonExistent;
} else {
echo 'class not found.';
}
like #prodigitalson said you can't catch that fatal error.
An error and an exception are not the same thing. Exceptions are thrown and meant to be caught, where errors are generally unrecoverable and triggered with http://www.php.net/manual/en/function.trigger-error.php
PHP: exceptions vs errors?
Can I try/catch a warning?
If you need to do some cleanup because of an error, you can use http://www.php.net/manual/en/function.set-error-handler.php
Thats not an exception, thats a FATAL error meaning you cant catch it like that. By definition a FATAL should not be recoverable.
Exception and Error are different things. There is an Exception class, which you are using and that $e is it's object.
You want to handle errors, check error handling in php-zend framework. But here, this is a Fatal error, you must rectify it, can not be handled.

php Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on construct

hey guys was hoping you could help me out..
just to let u know in advance, im a relatively new php coder, doing a practice project, and came across this problem and ive spent like an hour of rechecking and googling but just cant figure out whats causing it
error: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\forum\classes\ClassUser.php on line 7
the segment of the code causing the problem:
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){ //this is line 7
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
oh and since im new to php, incase im doing something which i should not be, please to recommend.. thanks in advance.
note:ive removed the php tags since they seemed to be messing with the formatting of this post :/
note2: im also getting another notice
Notice: Use of undefined constant KEY - assumed 'KEY' in C:\wamp\www\forum\classes\general.inc on line 20
but im assuming thats more of a warning than an error... but just adding incase it has something to do with the error
general.inc:
//error definations
define("ERROR_FIELD_EMPTY","Error! All required fields not filled");
define("ERROR_INVALID_SIGNIN","Error! Username/password do not match!");
define("ERROR_GENERAL_INPUT", "Error! Invalid input given");
define("ERROR_SQL_CONNECT","Error! Could not connect to sql database");
//field sizes
define("PASSWORD_LENGTH",12);
define("USERNAME_LENGTH",30);
//sql server details
define("SQL_SERVER_NAME","localhost");
define("SQL_SERVER_USERNAME","root");
define("SQL_SERVER_PASSWORD","");
define("SQL_SERVER_DATABASE","forums");
define(KEY,"key");
function __autoload($className){
require_once($_SERVER["DOCUMENT_ROOT"]."forum/classes/Class$className.php");
}
ClassUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
public function validate(){
if(strlen($user)>30|| empty($user) || preg_match('/[^a-zA-Z0-9]/i',$password==0 )){
throw new Exception(ERROR_GENERAL_INPUT);
}
$user=mysql_real_escape_string($user);
return true;
}
public function insert(){
// this->validate();
$conn= mysqli_connect(SQL_SERVER_NAME,SQL_SERVER_USERNAME,SQL_SERVER_PASSWORD,SQL_SERVER_DATABASE);
if(empty($conn)){
throw new Exception(ERROR_SQL_CONNECT);
}
$query="INSERT into USERS VALUES ($user,$password)";
$conn->query($query);
}
private $user;
private $password;
};
NewUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
try{
$user = new User($_POST['UserName'],$_POST['Password']);
$user->insert();
}
catch(Exception $Error){
echo $Error->getMessage();
}
Your __construct needs to have the word function in front of it, or better yet public function, in the same way as the other methods in the class (ie validate and insert in your case).
ie you need the following:
public function __construct($u,$p){ //this is line 7
Change the line to:
public function __construct($u, $p) {

PHP Unit Tests: Is it possible to test for a Fatal Error?

FWIW I'm using SimpleTest 1.1alpha.
I have a singleton class, and I want to write a unit test that guarantees that the class is a singleton by attempting to instantiate the class (it has a private constructor).
This obviously causes a Fatal Error:
Fatal error: Call to private FrontController::__construct()
Is there any way to "catch" that Fatal Error and report a passed test?
No. Fatal error stops the execution of the script.
And it's not really necessary to test a singleton in that way. If you insist on checking if constructor is private, you can use ReflectionClass:getConstructor()
public function testCannotInstantiateExternally()
{
$reflection = new \ReflectionClass('\My\Namespace\MyClassName');
$constructor = $reflection->getConstructor();
$this->assertFalse($constructor->isPublic());
}
Another thing to consider is that Singleton classes/objects are an obstacle in TTD since they're difficult to mock.
Here's a complete code snippet of Mchl's answer so people don't have to go through the docs...
public function testCannotInstantiateExternally()
{
$reflection = new \ReflectionClass('\My\Namespace\MyClassName');
$constructor = $reflection->getConstructor();
$this->assertFalse($constructor->isPublic());
}
You can use a concept like PHPUnit's process-isolation.
This means the test code will be executed in a sub process of php. This example shows how this could work.
<?php
// get the test code as string
$testcode = '<?php new '; // will cause a syntax error
// put it in a temporary file
$testfile = tmpfile();
file_put_contents($testfile, $testcode);
exec("php $tempfile", $output, $return_value);
// now you can process the scripts return value and output
// in case of an syntax error the return value is 255
switch($return_value) {
case 0 :
echo 'PASSED';
break;
default :
echo 'FAILED ' . $output;
}
// clean up
unlink($testfile);

Categories