I am trying to catch an error for my app in PHP any help? I'm not sure how to go about sorting this issue as I am pretty new to PHP.
Error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function SRP\App\App::onError(), 0 passed
Code:
namespace SRP\App;
use SRP\App\Librarys\Config\Config;
class App
{
public static function boot() {
Config::loadConfig();
set_error_handler(self::onError());
if (Config::get('app.environment') === 'development') {
error_reporting(E_ALL);
}
else {
error_reporting(0);
}
}
protected static function onError($exception) {
echo 'Something went wrong.';
}
}
Related
I created a class and try to call in a method, but however in my error log, I am getting response "Uncaught Error: Call to undefined method Customersss::throwError()", please what am I doing wrong, because I know I have created throwError(), I can't seem to be able to access the class.
Firstly the class trying to call object
$this->throwError(INVALID_DATA_TTT, "Invalid shipping fee"); //WHERE I SUSPECT ERROR IS BEING GENERATED
The full error
PHP Fatal error: Uncaught Error: Call to undefined method
Customersss::throwError() in
/home/osconliz/public_html/Osconlizapicall/customers.php:276 Stack
trace:
#0 /home/osconliz/public_html/Osconlizapicall/api.php(177): Customersss->insertNewDelivery()
#1 [internal function]: Api->create_insert_new_delivery()
#2 /home/osconliz/public_html/Osconlizapicall/rest.php(42): ReflectionMethod->invoke(Object(Api))
#3 /home/osconliz/public_html/Osconlizapicall/index.php(4): Rest->processApi()
#4 {main} thrown in /home/osconliz/public_html/Osconlizapicall/customers.php on line 276
customers.php
require_once('constants.php');
require_once('rest.php');
class Customersss {
private $id;
private $shipping_fee;
private $pickup_fee;
function setId($id){ $this->id = $id; }
function getId() { return $this->id; }
function setShipping_fee($shipping_fee){ $this->shipping_fee = $shipping_fee; }
function getShipping_fee() { return $this->shipping_fee; }
function setPickup_fee($pickup_fee){ $this->pickup_fee = $pickup_fee; }
function getPickup_fee() { return $this->pickup_fee; }
public function __construct(){
$db = new DbConnect();
$this->dbConn = $db->connect();
}
public function insertNewDelivery(){
if ($this->shipping_fee == ""){
$this->throwError(EMPTY_PARAMETER, "Empty shipping fee");
exit();
}
if ($this->shipping_fee == ""){
$this->throwError(INVALID_DATA_TTT, "Invalid shipping fee");
exit();
}
}
}
rest.php
require_once('constants.php');
class Rest {
protected $request;
protected $serviceName;
protected $param;
public function processApi(){
$api = new API;
$rMethod = new reflectionMethod('API', $this->serviceName);
if(!method_exists($api, $this->serviceName)){
$this->throwError(API_DOST_NOT_EXIST, "API does not exist");
}
$rMethod->invoke($api);
}
public function throwError($code, $message){
header("content-type: application/json");
$errorMsg = json_encode(['error' => ['status'=>$code, 'message'=>$message]]);
echo $errorMsg; exit;
}
}
constants.php
define('INVALID_DATA_TTT', 350);
define('EMPTY_PARAMETER', 404);
define('API_DOST_NOT_EXIST', 400);
define('ACCESS_TOKEN_ERRORS', 500);
api.php
require_once "customers.php";
require_once "constants.php";
class Api extends Rest {
public $dbConn;
public function __construct(){
parent::__construct();
$db = new DbConnect;
$this->dbConn = $db->connect();
}
public function create_insert_new_delivery(){
$shipping_fee= $this->validateParameter('item_category', $this->param['shipping_fee'], STRING, true);
try {
$cust = new Customersss;
} catch (Exception $e){
$this->throwError(ACCESS_TOKEN_ERRORS, $e->getMessage());
}
}
You are calling Rest::ThrowError() from Customersss class. It means your function is unreachable in this context.
To call this Rest method from the Customersss class, you can:
extend Rest to Customersss (see inheritance)
make ThrowError() a static method (see static)
Using inheritance:
class Customersss extends Rest { /* your properties/methods */ }
Using a static method:
class Rest {
static public function throwError($code, $message){ /* your code */ }
}
Callable with Rest::ThrowError()
I've got this weird error.
I've got DB class and Live class. the Live class is not extends DB class.
I can't understand why when i declare the New Live this happened.
The Errors is:
Notice: Undefined property: Live::$isOff in /home/main/Database/DB.class.php on line 570
Fatal error: Call to undefined method Live::Query() in /home/main/Database/DB.class.php on line 574
This is what i got on this line:
ON DB.Class.php
public function setCharset($charset) {
if (self::$isOff){
return false;
}
self::$charset = $charset;
self::Query("SET NAMES '".$charset."'");
return self;
}
ON Live.Class.php
class Live
{
protected $PaypalLogo = '';
protected $isQA = null;
protected $siteURL = null;
public function __construct() {
DB::setCharset("utf8");//Setting charset here and that is gives Fatal Error.
if (strstr($_SERVER['SERVER_NAME'], "ba.")) {
$this->isQA = true;
$this->siteURL = "http://ba.live.com";
}
else {
$this->isQA = false;
$this->siteURL = "http://www.live.com/";
}
}
Thank you for any advice.
I found the Problem. it was because there are no instance of DB and because of this, it was take the instance of Live .
I am getting this error when I try to run my script:
Fatal error: Call to a member function is_connected() on a non-object
in C:\xampp\htdocs\DataMigrator\ci_my_app\models\ReadData_model.php on
line 7
<?php
class ReadData_model extends CI_Model {
public function get_table($target) {
if ($target != 'sender' && $target != 'receiver') { exit('Error: Illegal parameter. Please use sender or receiver instead.'); }
$this->load->model('Connection_model');
if ($this->connection_model->is_connected($target)) { // This is line #7
return $this->connection_model->get_custom_db($target)->list_tables();
}
else {
return false;
}
}
}
is_connected():
public function is_connected($target) {
if ($this->get_custom_db($target)) {
return true;
}
else {
$this->session->unset_userdata('connection');
return false;
}
}
What am I doing wrong?
When you call the connection_model try capitalizing the first letter since you loaded it with one $this->load->model('Connection_model');
So when you use it it should be:
$this->Connection_model->is_connected($target))
I know it sounds stupid, but I have had some funky things happen like this while using Codeigniter.
Check if $this->connection_model is properly initialized.
I am reading a book about php and I found this script that doesn't work:
namespace woo\controller {
// woo\controller\ApplicationHelper;
class ApplicationHelper {
function getOptions() {
if (!file_exists("data/woo_options_not_there.xml")) {
$r = new \woo\base\AppException("Non riesco ad aprire il file<br>");
throw new $r;
}
$options = simplexml_load_file("data/woo_options.xml");
$dsn = (string) $options->dsn;
print $dsn;
}
}
$d = (new ApplicationHelper())->getOptions();
}
namespace woo\base {
class AppException extends \Exception {
}
}
Can you help me?
This is the mistake:
Fatal error: Uncaught exception 'woo\base\AppException' in C:\xampp\htdocs\9781430260318_Chapter_12_Code\listing12.00.php:11 Stack trace: #0 C:\xampp\htdocs\9781430260318_Chapter_12_Code\listing12.00.php(20): woo\controller\ApplicationHelper->getOptions() #1 {main} thrown in C:\xampp\htdocs\9781430260318_Chapter_12_Code\listing12.00.php on line 11
So let's follow the train of logic here
We're creating the class ApplicationHelper and then instantiating it. Then, we call getOptions() which looks for an XML file and, if it doesn't find it, throws an exception.
Your code is working fine, you just forgot to catch the exception (this produces a fatal PHP error)
try {
$d = (new ApplicationHelper())->getOptions();
} catch(\woo\base\AppException $err) {
echo 'Error: ' . $err->getMessage();
}
I am writing a config file parser and have a function called getVals() in my Config.php file but apparently when I call it in a test it throws an "Undefined function" error.
Config.php
<?php
require_once '../extlib/pear/Config/Lite.php';
class Config {
private $config;
function __construct($conf) {
$this->config = new Config_Lite();
echo "calling open...<br>";
$this->open($conf);
echo "open done...<br>";
}
function open($cfile) {
if (file_exists($cfile)) {
$this->config->read($cfile);
} else {
file_put_contents($cfile, "");
$this->open($cfile);
}
}
function getVals() {
return $this->config;
}
function setVals($group, $key, $value) {
$this->config->set($group, $key, $value);
}
function save() {
$this->config->save();
}
}
?>
Testing class in cfgtest.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once '../util/Config.php';
$cfile = "../../test.cfg";
$cfg = new Config($cfile);
if (is_null($cfg)) {
echo "NULL";
} else {
echo $cfg.getVals();
}
?>
Output
calling open...
open done...
Fatal error: Call to undefined function getVals() in cfgtest.php on line 13
I would like to know why the undefined function error appears when there's the function there already.
In php to call a method or a member of an object, use the -> operator:
if (is_null($cfg))
{
echo "NULL";
}
else
{
echo $cfg->getVals();
}
Learn more on PHP Object Oriented Programming on PHP's website.
Call should be using -> operator
$cfg.getVals();
should be
$cfg->getVals();
Use $cfg->getVals(); instead of $cfg.getVals();
Now you are trying to do a concatenation !
Ooops ... missed the '->'. Lol. Good catch for all.