I want to make my own class in php to handle my errors I want to show to the user, for example when the user are logging in but the password or username is false.
I have a map classes with the files:
errorHandling.class.php
class errorHandling
{
public $customError;
public function setCustomError($error)
{
$this->customError = $error;
}
public function getCustomError()
{
echo $this->customError;
}
}
And in the users.class.php
when the user try's to login and it fails the else will be like:
else {
include 'errorHandling.class.php';
$errorHandle = new errorHandling();
$errorHandle->setCustomError("Username or password are wrong!");
}
Then in the login.php in the root map I have this code to call the function:
include 'classes/errorHandling.class.php';
include 'classes/users.class.php';
$errorHandle = new errorHandling();
$errorHandle->getCustomError();
Well now I get this error message but I don't understand it so I hope some of you guys can helping me out or give me some tips to improve my class.
Fatal error: Cannot declare class errorHandling, because the name is already in use in /classes/errorHandling.class.php on line 1
Every time you include a file in php, it will be loaded and run. For class definitions, you want to use require_once, which as it says, will only be loaded once.
Related
I am developing an app where I need to log the proccess.
So I was loggin from main.php and now I need to log from another class (class_TestingLog.php)
Next code is how I am trying. Could you tell me what I am doing wrong?
Thank you in advance.
main.php
[...]
#Logging class
include_once("./classes/class_log.php");
$Class_Log = New Class_Log();
#TestingLog Class
include_once("./classes/class_testingLog.php");
$Class_TestingLog = New Class_TestingLog();
[...]
$Class_Log->FreeLog("Test log");
$Class_TestingLog->AnyFuncion();`
[...]
class_log.php
[...]
public function FreeLog($text)
{
// echo $text . "\n"; #mistake
$this->outputText .= text; #correct one
}
[...]
class_testingLog.php
private $Class_Log = '';
[...]
public function __construct()
{
#Requires
require_once("./classes/class_log.php");
$this->Class_Log = New Class_Log();
}
public function AnyFuncion()
{
var_dump($this); #From real file
$this->Class_Log->FreeLog("Test log from another classs");
}
[...]
Browser output
Test log
Browser output expected
Test log
Test log from another classs
===========================================================================
EDIT
I made an error copying FreeLog();
It stores the parameter string value into a private variable instead echo the variable.
You require_once statement inside __construct for Class_TestingLog is invalid and unnecessary. As both files are in the same directory it should be "class_log.php" not "./classes/class_log.php". There is no need for it anyway, as when you include them both in main.php it is loaded already. So try it without the require_once.
EDIT: As discussed in chat do it like this:
in main.php:
$Class_TestingLog = New Class_TestingLog($Class_Log);
in class_testingLog.php:
public function __construct($ClassLog)
{
$this->Class_Log = $ClassLog;
}
This will use the same instance inside the Class_TestingLog class as on the main.php.
I'm trying to write an external library which has functions commonly used among various classes.
Currently I'm trying to write a log message function. The problem is I need access to the session library and a model. How do I access them without extending from CI_Controller? Any workaround?
Here's my code:
Common_functions library:
public function _send_message($message, $log_to_db=TRUE)
{
$this->session->set_userdata("message", $message);
if($log_to_db) $this->User_log_model->log_mesage($message);
}
Usage in other classes example:
public function new_user()
{
$this->_set_validation_rules();
if($this->form_validation->run())
{
if($user_id = $this->User_model->insert($this->_prepare_new_user_array()))
{
$this->common_functions->_send_message("New User created successfully. | user_id: " . $user_id);
}
else {
$this->common_functions->_send_message("Unable to create new User record.");
}
}
}
Managed to solve it. Just moved my log message functions to the User_log_model instead.
I've researched this and found other questions (PHP class not found but it's included) but they don't answer this stupidly simple problem!
Here's the error: Class 'Functions' not found in /public_html/sites/sitename/index.php
My folder structure is like this:
public_html/assests/php/functions.php //class
I require_once this file from here: public_html/sites/sitename/index.php
I've checked the file_exists, of course it does else require_once would fail. What is going on?!
Here's my code (just in case I've missed something really dumb...)
require_once('../../assets/php/functions.php');
if( file_exists('../../assets/php/functions.php') ){
error_log('Of course it exists!');
}
else{
error_log('Oh dear...');
}
$fn = new Functions();
Contents of functions.php (edited for brevity):
class Functions{
public function functions(){
//constructor
}
//pretty print
public function pp($str){
return '<pre class="pre-show prettyprint">'.htmlentities($str).'</pre>';
}
}
You have a misspelling, an extra s in 'assets':
My folder structure is like this:
public_html/assests/php/functions.php //class
Make sure your file name is "assets" and make sure your require matches that:
require_once('../../assets/php/functions.php');
I am creating website in PHP. I am using MVC in PHP. My website works like this, if user go to example.com/about then it it will load About class and index() function. If user will go to localhost/about/founder then it will load founder() function from About class. but the thing is that if I go to localhost/About or localhost/AbOut or anything like that it is loading default index() function from About class file. So what to do with case sensitivity? I mean I want my script to load index() function from class file if it is localhost/about or localhost/terms. If anything is in uppercase, then it should load 404 error function. 404 error function is already set in my site.
Please help me friends.
here is my Bootstrap.php class file
<?php
/*
Bootstrap class to run functions by URL
*/
class Bootstrap {
public $_req;
public $_body;
public $_file;
public $_error;
function __construct(){
if(empty($_GET['req'])){
require 'classes/home.php';
$this->_body = new Home();
$this->hdr($this->_body->head());
$this->_body->index();
$this->ftr();
exit();
}
$this->_req = rtrim($_GET['req'], '/');
$this->_req = explode('/', $this->_req );
$_file = 'classes/'.$this->_req[0].'.php';
if(file_exists($_file)){
require $_file;
}
else {
$this->error(404);
}
$this->_body = new $this->_req[0];
$this->hdr($this->_body->head());
if(isset($this->_req[2])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}($this->_req[2]);
}else {
$this->error(404);
}
}else {
if(isset($this->_req[1])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}();
}else {
$this->error(404);
}
}else {
$this->_body->index();
}
$this->ftr();
}
}
//this function is to set header in html code
public function hdr($var = false){
echo '<!DOCTYPE HTML><html><head>'.$var.'</head><body>';
}
//this function is tp set footer in html code
public function ftr($var = false){
echo $var.'</body></html>';
}
//error handler
public function error($var){
require 'classes/er_pg.php';
$this->_error = new Error();
$this->_error->index($var);
}
}
You shouldn't use anything to load non-lowercase URLs because of the duplicate content, and that's a good thing you're doing. The wrong URLs should fail automatically in such cases.
However, since you didn't show how are you making those calls, then only thing I can suggest at this point is to check if the called method exists (case-sensitive), and if not, throw/redirect to a 404 page (header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");).
UPDATE
After all the chat in the comments, seems like file_exists is not case-sensitive in your case, which is really weird. Hopefully someone will be able to figure it out so I can delete this (keeping it because of the info in the comments).
I solved the problem. I used this
if(ctype_lower($this->_req[0])){
$_file = 'classes/'.$this->_req[0].'.php';
and now its working. Thanx anyways friends.
I have just created a class to control my php application, and I have one big problem ( I use 2 days for thinking and searching about it but can't find any solutions). My class contains a method named register(), which load scripts into pages. My class is:
class Apps
{
protected $_remember; // remember something
public function register($appName)
{
include "$appName.php"; //include this php script into other pages
}
public function set($value)
{
$this->_remember = $value; // try to save something
}
public function watch()
{
return $this->_remember; // return what I saved
}
}
And in time.php file
$time = 'haha';
$apps->set($time);
As the title of my question , when I purely include time.php into main.php, I can use $apps->set($time) ($apps has been defined in main.php). Like this main.php:
$apps = new Apps();// create Apps object
include "time.php";
echo $apps->watch(); // **this successfully outputs 'haha'**
But when I call method register() from Apps class to include time.php , I got errors undefined variable $apps and call set method from none object for time.php (sounds like it doesn't accept $apps inside time.php to me) . My main.php is:
$apps = new Apps();// create Apps object
$apps->register('time'); // this simply include time.php into page and it has
//included but time.php doesn't accept $apps from main.php
echo $apps->watch(); // **this outputs errors as I said**
By the way , I'm not good at writing . So if you don't understand anything just ask me. I appreciate any replies. :D
If you want your second code snippet to work, replace the content of time.php with:
$time = 'haha';
$this->set($time); // instead of $apps->set($time);
since this code is included by an instance method of the Apps class, it will have access to the instance itself, $this.