I have a problem with classes which cannot be found in PHP.
The first thing I do is 'require_once' a file which 'require_once's all other files. When loading, no problems are showed. But when I start calling my function (Users::verificate();) I get the following error:
Fatal error: Class 'Users' not found in /Applications/XAMPP/xamppfiles/htdocs/sparks/dashboard.php on line 4
To test I've added a simple class with a function which only outputs a string with the echo method. This works so the problem has to be with this class. A MySql function which I call like this just works.
$mySql = new MySql();
$mySql->executeQuery('...');
The simple class has static a static function which I call like this (Oh, this works):
simple::launch();
In the Users class I'm calling non static functions from the MySql class from a static function. Can this be the problem?
Like another question here on SO suggested the problem isn't in using short php opening tags instead of the traditional php opening tag.
Even a little hint may help me :). Thanks for your time!
Edit:
I've added some relevant code from the User class. This is basically what it all looks like:
<?php
class Users {
public static function authenticate($email, $password) {
$mySql = new MySql();
$mySqlResult = $mySql->executeQuery("selectUser", [$email]);
...
}
public static function isAdmin() {
if ($_SESSION['isAdmin']) {
return true;
}
return false;
}
...
}
Edit 2:
I'm trying to show the flow:
From dashboard.php this are the first code lines:
<?php
require_once('code/init.php');
simple::launch();
if (!Users::verify()) {
header("Location: index.php");
}
?>
simple::launch(); is the code I used to test. This executes well. From this on the init.php file looks like this:
<?php
session_start();
require_once('simple.php');
require_once('MySql.php');
require_once('Users.php');
require_once('Projects.php');
The file names are correct as I get a visible error when these are wrong.
dashboard.php exists in the root. From there is a folder called 'code' which contains all these files.
#MbRostami gave the advice in a comment to use the 'get_required_files()' function to see which files are included. It turns out that the wrong files were loaded.
Root
| dashboard.php
| users.php
|- code
| Users.php
| init.php
In the init.php file the Users.php file (both files are in the code folder) was required. But for some reason the users.php file from the root was loaded. Some strange behaviour imho. Ahwell, that's something to investigate during the christmas days.
Problem is solved! Thanks!
Related
So I've been in the process of updating someones old CI 1 to CI 3 code. In process. In particular, the URI class extension is not working. I've read the CI documentation switched to __construct() and moved it to the application/core directory. I've checked SO and all cases are correct, but I still get the following error:
Call to undefined method MY_URI::last()
My code below
class MY_URI extends CI_URI {
function __construct()
{
parent::__construct();
}
function last()
{
return $this->segment(count($this->segments));
}
}
Thoughts as to why this may be happening with the switch? Checking StackOverflow it said chek your config settings by the config has the correct
$config['subclass_prefix'] = 'MY_';
I'm calling it with:
$lastURI = $this->uri->last();
Update: I've also tried the
exit('MY_URI.php loaded');
trick at the top which seems to work, but it still throws the error when I remark it out and never loads the extension.
Place your MY_URI.php file inside the application/core/MY_URI.php & update the function like following.
public function last(){
return $this->segment($this->total_segments());
}
call it like below
$last = $this->uri->last();
I have a php class that uses "include" to load some html and php from a file. Within that file I want to access the class object that included the file, but I keep getting "Fatal error: Call to a member function makeSizesSelect() on a non-object ..."
I've tried both include and require, I've tried declaring globals, I've tried everything I can think of and everything I've so far found on SO. Nothing seems to allow the file I include to have php code access the object that included it.
Any ideas?
Here's a few snippets ...
The class file:
class cdf {
public $version = 001;
public function cdf_shortcode( $atts,$content )
{
$this->slog( 2,"shortcode() case: show" );
require( 'templates/container.php' );
}
}
And the required file container.php contains the following (amongst other stuff):
<?php
echo "version = ".$this->version;
?>
I then try to use the object:
$cdf = new cdf();
$cdf->cdf_shortcode( null, null);
The line $this->slog( 2,"shortcode() case: show" ) works. It runs that function (which I haven't included in this snippet) just fine. But then the file I require (or include) cannot use $this to access the object. I'm at a loss. :-(
All I want to do is access within the included file, the variables and methods in the class that included the file ...
Sorry, some added information. I'm not sure if this makes any difference. The code above is all part of a WordPress plugin.
Curious issue with a curious solution. I finally found the answer over here:
Possible to access $this from include()'d file in PHP class?
I tried all the obvious solutions this poster tried (globals, casting to another variable, etc) with the same lack of success. Turns out, just changing the file extension from .php to .tmpl fixed the issue, and my included file can now access the object that included it. Weird. (Of course, the downside now is that my IDE doesn't colour my code for me. :-( )
Thanks for your suggestions guys.
In the file you included you need to instantiate the class.
<?php
$yourClass = new cdf();
echo "version = ".$yourClass->version;
?>
When you want to access a function in a class, you need to instantiate the class first otherwise you wont have access to anything inside of it.
Also make sure the file you are including wont be included anywhere else where the class cdf doesn't exist because that will result in an error.
The variable $this can only access methods, variables, etc. only if they are in the same object.
Update based on your answer that seems to have worked:
Example.php
<?php
echo $this->returnString();
echo $this->randomVariable;
File.php
<?php
class IncludedClass
{
public $randomVariable = 123;
public function returnString()
{
return "some random string";
}
public function meh()
{
require_once('Example.php');
}
}
$meh = new IncludedClass();
$meh->meh();
I have a problem while including my own php class page from its server.
I hosted my php classes page like http://my.website.com/phpclasses.php
and tried to call it from another host such as:
<?php
include ("http://my.website.com/phpclasses.php");
?>
By the way, the php class page has a class named 'test'
and activated in it by this variable, here is a quick view about it:
phpclasses.php
class test{
somefunctions();
somefunctions();
}
$test = new test();
I requested this page from another website like this:
anotherwebsite.php:
$newclass = new test();
$newclass->somefunctions();
Notice that i've activated the class twice, once in its page, and again in the another website, but failed and returns nothing.
How could it work?
It's a bad idea to include over HTTP unless your printing PHP Code to the browser. Otherwise it will not work.
It's best to include locally:
Class.php
class Test{
public function __construct(){
return TRUE;
}
}
index.php
include "Class.php";
$Class = new Test();
Both files are located on the same server (same directory).. if including in other directories, add correct path to the file:
include "/Core/Class.php";
Being interpreted as
/ - Root Directory
Core - Sub directory
Class.php - File name
I'm going crazy over these past few days and I really need your help because I don't know what else to do. I tried everything.
Here is the problem:
I have two files. site.php and logs.php.
Here is the content of logs.php:
class Logs {
function __construct() {
}
}
Now comes the fun part, beginning of 'site.php':
require_once("database.php");
require_once("logs.php");
class Site {
private $db;
private $logs;
function __construct() {
$this->logs = new Logs(); ### this is the error line
$this->db = new MySQLDatabase();
}
}
They are all in the same directory. Why can't it find my class?
PHP might be including the wrong the file. PHP will search the include path before looking inside the script's directory. You may somehow have a logs.php in your include path (See This PHP manual for details.) I suggest maybe adding an echo "hello world"; in the root of your logs.php.
You might also have accidentally enclosed the class inside another class or function. Make sure the class is only inside the tags.
Thank you all for helping. I have found out what the problem was! Turns out, for some reason I shouldn't name my php file 'logs.php'. As soon as I renamed it, it works perfectly. I have no idea why but it just wont work with that name.
I'm having trouble with accessing the session in an external .php script located in webroot.
Thought I'd write a function getSession() in one of my controllers and try to call it in the .php file.
So in steps:
I have file.php
In a controller I have a function getSession().
How to call the controllers function in the file.php?
Thank you.
EDIT
Meanwhile I fixed my bug, but still am curious how this is done and want other stack users to find a good answer to this so:
Its exactly like this:
In UsersController I have a function:
public function getSession() {
return $_SESSION['Auth']['User']['user_id'];
}
That I want to let's say print (for example) like this: print_r(Users.getSession) in the file test.php located in webroot/uploadify/test.php.
This file is not a class, but if it is required, then it shall be :)
#CaboOne: Maybe your answer was correct, I just wasnt sure what code to call (and enter) where :)
Supposed I have the following php file in webroot folder:
<?php
class TestingClass {
function getName(){
return "Test";
}
}
?>
I would do the following:
// This would bring you to your /webroot folder
include $_SERVER['DOCUMENT_ROOT'].'/another_file.php';
// Initializing the class
$example = new TestingClass;
// Call a function from the initialized class
$a_value = $example->getName();
// If you want to use $a_value in the view, you can then set
$this->set('a_value', $a_value);