Php Fatal Error Class not found - php

I am trying to debug an issue with a php website that was working and for some reason stopped. When I try to access the website via http it does not work so I check the server logs in Linux and this is what I get:
PHP Fatal error: Uncaught Error: Class 'HomePageBanner' not found
The beginning of the file has code
<?php
require_once 'config.php';
include "checkiflogin.php";
$condition = "";
$objDreamVacationGallery = new HomePageBanner();
$data = $objDreamVacationGallery->selectAllRecords($condition, $sort_field, $sort_order, $start, $limit);
?>
The index.php is stored in the root directory and the HomePageBanner is stored in root/classes. The code for HomePageBanner looks something like
<?php
class HomePageBanner extends DataBase
{
public $db_table = 'tblxxx';
public $data = '';
public function __construct($data=''){ /* VALUE ASSIGNMENT */
parent::__construct();
if($data!=''){
$this->data = $data;
}
}
public function __destruct(){
parent::__destruct();
}
Would appreciate any help.
Thanks

You need to require_once homepagebanner class first as below
require_once('classes/HomePageBanner.php');
and as i seen in comments you are using linux make sure classes and HomePageBanner in code are in the same case as in the your directory /var/www/html

Related

Access model from Thread in Yii

I have to parse a huge csv files in a Yii 1.1 Application.
Each row has to be validated and saved to the database.
I decided to use Multi Threading for this task.
So here is my code in the Controller action:
public function parseData($) {
$this->content = explode("\n", $this->content);
$thread_1 = new DatalogThread(array_slice($this->content, 0, 7000));
$thread_2 = new DatalogThread(array_slice($this->content, 7001));
$thread_1->start();
$thread_2->start();
}
And the Thread (I put it in models folder):
class DatalogThread extends Thread {
public $content;
public function __construct($content) {
$this->content = $content;
}
public function run() {
foreach ($this->content as $value) {
$row = str_getcsv($value);
$datalog = new Datalog($row);
$datalog->save();
}
}
}
The problem is that the Thread does not get access to the model file:
Fatal error: Class 'Datalog' not found in C:\xampp...\protected\models\DatalogThread.php
I tried Yii::autoload("Datalog"), but got The following error:
Fatal error: Cannot access property Yii::$_coreClasses in ...\YiiMain\framework\YiiBase.php on line 402
Yii uses a LOT of statics, this is not the best kind of code for multi-threading.
What you want to do is initialize threads that are not aware of Yii and reload it, I do not use Yii, but here's some working out to give you an idea of what to do:
<?php
define ("MY_YII_PATH", "/usr/src/yii/framework/yii.php");
include (MY_YII_PATH);
class YiiThread extends Thread {
public $path;
public $config;
public function __construct($path, $config = array()) {
$this->path = $path;
$this->config = $config;
}
public function run() {
include (
$this->path);
/* create sub application here */
}
}
$t = new YiiThread(MY_YII_PATH);
$t->start(PTHREADS_INHERIT_NONE);
?>
This will work much better ... I should think you want what yii calls a console application in your threads, because you don't want it trying to send any headers or anything like that ...
That should get you started ...

Can not call global function from php class

I have a php project that uses some functional elements, and some OOP elements, but it seems mixing the two is causing problems. Here are the files that are causing the errors:
DB.php
<?php
function parse_db_entry($from, &$to){
//Function code here
}
?>
User.php
<?php
require_once 'DB.php';
class User{
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
Everything works as it should, except the call to parse_db_entry which throws:
Fatal error: Call to undefined function parse_db_entry()
I am able to access other things in DB.php, for instance if I made a class it there I am able to instantiate it without error, and if I move the function into User.php, it is functional as well. So what am I doing wrong? Why can't I call this method?
I've figured it out! Thanks to everyone who had ideas, but it seems the problem was something else.
When calling require_once 'DB.php', php was actually getting the file:
C:\xampp\php\pear\DB.php
instead of mine.
This may be a problem exclusive to XAMPP, but a simple rename of my file to DBUtil.php fixed everything.
This is a stretch, and I'm totally taking a shot in the dark here, but...
Are you sure parse_db_entry is in the global or User's namespace?
Note: I added a few lines here and there for testing/debugging.
DB.php:
<?php
namespace anotherWorld; // added this ns for illustrative purposes
function parse_db_entry($from, &$to){
echo 'called it';
}
?>
User.php:
<?php
namespace helloWorld; // added this ns for illustrative purposes
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
test.php:
<?php
require_once 'DB.php';
require_once 'User.php';
use helloWorld\User;
$a = new User();
$a->load_user('email','pass');
echo 'complete';
?>
Yields Fatal error: Call to undefined function helloWorld\parse_db_entry() in User.php on line 13, however when removing the NS declaration in DB.php (namespace anotherWorld) thereby putting parse_db_entry in global NS it runs just fine.
To verify, use the __NAMESPACE__ constant.
If namespace is a problem, without compromising DB's namespace, here is an updated User.php:
<?php
namespace helloWorld;
use anotherWorld; // bring in the other NS
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
anotherWorld\parse_db_entry($entry, $user); // call the method from that NS
return $user;
}
}
?>

variable ambit and sending parameters in php OO

I'm very confused about using the parameters through pages with PHP OO.
I'm following a tutorial about creating a framework (it's just like the Zend Framework); but, what I don't understand is when this happens:
Example, the index:
// File: sourcefiles/index.php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', realpath(dirname(__FILE__)).DS );
define ('APP_PATH',(ROOT.'aplicacion'));
require_once APP_PATH. DS.'Config.php';
require_once APP_PATH. DS.'Request.php';
require_once APP_PATH. DS.'BootStrap.php';
require_once APP_PATH. DS.'Controller.php';
require_once APP_PATH. DS.'View.php';
try
{
BootStrap::run(new Request());
I have:
// File: sourcefiles/controladores/IndexController.php
<?php
class IndexController extends Controller
{
public function __construct() {
parent::__construct();
}
public function indexAction()
{
$this->view->titulo='Homepage';
$this->view->contenido='Whatever';
$this->view->renderizar('index');
}
}
?>
And this:
// file : sourcefiles/aplicacion/View.php
<?php
class View
{
private $controlador;
private $layoutparams;
public function __construct(Request $peticion)
{
$this->controlador = $peticion->getControlador();
}
public function renderizar($vista,$item=false)
{
$rutaview = ROOT.'vistas'.DS.$this->controlador.DS.$vista.'.phtml';
if (is_readable($rutaview))
{
include_once $rutaview;
}
else
{
throw new Exception('Error de vista');
}
}
}
?>
And here is the View:
// file : sourcefiles/vistas/index/index.phtml
<h1>
Vista index..
<?php
echo $this->titulo;
echo $this->contenido;
?>
</h1>
Now my questions are:
How the IndexController can use the line? $this->view->titulo = blabla;
The view class doesn't have a "titulo" attribute; however, I can do that. But here is a curious thing, if I do that after calling the $this->view->renderizar('index'), I get the error.
How does the index.phtml file knows this? echo $this->titulo; because, there isn't a include or require called, it's confusing to me.
When I do a require or include call in a file, the required or included file knows the caller's variables?
If somebody can explain these to me, I would really appreciate it :D
or link me to a discussion on official information about this, or how is this called?
Think of an include or require line as "copy-and-pasting" the code from one file into another. This isn't quite accurate, but it explains part of the behaviour here:
In sourcefiles/aplicacion/View.php you include sourcefiles/vistas/index/index.phtml while inside the function View->renderizar. So all the code in index.phtml gets loaded as though it was happening inside that function too. This is why you can access $this, for instance.
As for referencing $this->view->titulo when you haven't defined it, this is PHP letting you be lazy. Just like any variable, a member on an object will spring into life as soon as you mention it, with only a notice warning that maybe you made a mistake.

PHP - Fatal error: Class 'PHPUnit_Framework_Test

So I have started to use PHPUnit to test my programs.
I have this problem where I get an error when I try to test a program where the program is gonna control if a webpage exists.
The code:
<?php
class RemoteConnect
{
public function connectToServer($serverName=null)
{
if($serverName==null){
throw new Exception("That's not a server name!");
}
$fp = fsockopen($serverName,80);
return ($fp) ? true : false;
}
public function returnSampleObject()
{
return $this;
}
}
?>
And the test code to it:
<?php
require_once('RemoteConnect.php');
class RemoteConnectTest extends PHPUnit_Framework_TestCase
{
public function setUp(){ }
public function tearDown(){ }
public function testConnectionIsValid()
{
// test to ensure that the object from an fsockopen is valid
$connObj = new RemoteConnect();
$serverName = 'www.google.com';
$this->assertTrue($connObj->connectToServer($serverName) !== false);
}
}
?>
They are in the same directory named: PHPUnit inside the www (C:\wamp\www\PHPUnit)
But I don't understand why i get the error (Fatal error: Class 'PHPUnit_Framework_TestCase' not found in C:\wamp\www\PHPUnit\RemoteConnectTest.php on line 5)
My PHPUnit package path is (C:\wamp\bin\php\php5.3.10\pear\PHPUnit)
I have tried making a program MailSender, where it sends a mail with a text content in it, that was just for using PEAR. And it succeded, but I don't understand why this doesn't work.
Regards
Alex
Don't you need to have the PHPUnit_Framework_TestCase class available in RemoteConnectTest.php?
Add the following on top of the file:
require_once 'PHPUnit/Autoload.php';

CodeIgniter: get_instance inside My_Lang

I found this useful Internationalization code:
http://pastebin.com/SyKmPYTX
everything works well except I am unable to use CI functions inside this class .
I want to set $languages and $special variable from DB .
but when I am using $CI =& get_instance(); in instance function its showing following error :
Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 231
The language class is loaded before the CodeIgniter instance exists, which is why you get the error.
You can use a post_controller_constructor hook to set your variables.
Here is a thread from the CodeIgniter forums where someone is tried to do something similar: http://codeigniter.com/forums/viewthread/108639/
The easiest way
in My_Lang.php
var $languages = array();
function __construct()
{
parent::__construct();
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->query( 'SELECT * FROM languages');
$result = $query->result();
foreach( $result as $row )
{
$this->languages[$row->short_name] = $row->full_name;
}
}
i did this and is working fine :)) i also added default_uri in foreach.

Categories