So I have four classes in a model directory but my index.php file for a web application can not access the classes. The IDE keeps telling me my that my classes are undefined and when I run the web-app I get these errors:
Notice: Undefined variable: CategoryDB in C:\xampp\htdocs\ex_starts\ch14_ex1\product_manager\index.php on line 28
Fatal error: Call to a member function getCategory() on a non-object in C:\xampp\htdocs\ex_starts\ch14_ex1\product_manager\index.php on line 28
I thought using require statements would include all the classes and their functions but I seem to have made a mistake. Could someone point out where I went wrong?
Here is the index.php require statements:
<?php
require('../model/database.php');
require('../model/category.php');
require('../model/category_db.php');
require('../model/product.php');
require('../model/product_db.php');
$CategoryDB = new CategoryDB();
$ProductDB = new ProductDB();
$action = (if and else statements with class method calls below) {
if ($action == 'list_products') {
$category_id = filter_input(INPUT_GET, 'category_id',
FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == FALSE) {
$category_id = 1;
}
// Get data (the CategoryDB and ProductDB have an undefined class warning here)
$current_category = $CategoryDB->getCategory($category_id);
$categories = $CategoryDB->getCategories();
$products = $ProductDB->getProductsByCategory($category_id);
else if{
//code here
}
}
?>
Class names are correct but every function call gives the same class undefined warning in my IDE. Any help in understanding what the correct way to do this would help and be appreciated, Thanks!
Related
I'm stuck in this error. Can someone help me out? I am using Codeigniter 3.1.10
Here's a snippet of my code in system/core/Common.php
function &load_class($class, $directory = 'libraries', $param = NULL)
{
static $_classes = array();
// Does the class exist? If so, we're done...
if (isset($_classes[$class]))
{
return $_classes[$class];
}
$name = FALSE;
// Look for the class first in the local application/libraries folder
// then in the native system/libraries folder
foreach (array(APPPATH, BASEPATH) as $path)
{
if (file_exists($path.$directory.'/'.$class.'.php'))
{
$name = 'CI_'.$class;
if (class_exists($name, FALSE) === FALSE)
{
require_once($path.$directory.'/'.$class.'.php');
}
break;
}
}
I keep on receiving this error message:
Notice: Only variables should be passed by reference in
/customers/5/d/b/tsoft.se/httpd.www/kanban/system/codeigniter/Common.php
on line 148
A PHP Error was encountered Severity: Notice
Message: Only variables should be passed by reference
Filename: codeigniter/Common.php
Line Number: 148
**Note : Line 148 is this return $_classes[$class];
As stated in a comment on this github issue, since you are using CodeIgniter version 3.1.10, you need to replace your system folder.
The file system/codeigniter/Common.php has been moved to system/core/Common.php on your version.
I'm trying to catch the URL from my localhost, here it is
http://localhost/mvc/index.php?url=Index/category and things are going well but when I'm trying to use the URL /category it is showing error. Here is the error
Notice: Array to string conversion in C:\xampp\htdocs\mvc\index.php on line 21
Notice: Undefined property: Index::$Array in C:\xampp\htdocs\mvc\index.php on line 21
Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\mvc\index.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mvc\index.php on line 21
<?php
include_once "system/libs/main.php";
include_once "system/libs/Dcontroller.php";
include_once "system/libs/Load.php";
?>
<?php
$url = isset($_GET['url']) ? $_GET['url'] : NULL;
if ($url != NULL) {
$url = rtrim($url,'/');
$url = explode("/", filter_var($url,FILTER_SANITIZE_URL));
} else {
unset($url);
}
if (isset($url[0])){
include 'app/controllers/'.$url[0].'.php';
$ctlr = new $url[0]();
if (isset($url[2])) {
$ctlr->$url[1]($url[2]);
} else {
if (isset($url[1])) {
$ctlr->$url[1](); //Here is the line where I'm getting the
error
} else {
}
}
}else{
include 'app/controllers/Index.php';
$ctlr = new Index();
$ctlr->home();
}
?>
But when I'm using
category() instead of $url[1]
it's working fine. Here is the Index class.
<?php
class Index extends Dcontroller
{
public function __construct()
{
parent::__construct();
}
public function home()
{
$this->load->view("home");
}
public function category()
{
$data = array();
$catModel = $this->load->model("CatModel");
$data['cat'] = $catModel->catList();
$this->load->view("category", $data);
}
}
two things straight away: "/" is not legal in a url param as part of a get string. you need to encapsulate it with URL encodeing
EG:
http://localhost/mvc/index.php?url=Index%2Fcategory
it's also the fact that "$ctlr->$url[1]" simply doesn't have a function called it.. eg: whatever "$ctlr->$url[1]" resolves to ??category()?? doesn't exist, you need to MAKE it.
add this to your code
function category() {
Index tmp = new Index();
tmp->category();
}
EDIT : I've just noticed, it's even more idiotic than I thought.. your string says Index/category doesn't it?.. make the class method static.. (this code is dreadful by the way it displays almost no sound knowledge of design whatsoever) there is no Index/category because you can't call category inside a class except if it's a static method.
Learn to code.
Trying to crate objects dynamically for a plug in system (work in progress)
heres my code using $this->module->load_module('test'); to use the method that creates the dynamic objects. Following code is the function that loads the class's and makes use of an auto loader, i have checked that its getting the correct file etc.
<?php
class BaseModule {
function __construct() {
}
function load_module($module){
echo 'Module = '.$module.'<br />';
$object_name = $module . "Controller";
$this->$$module = new $object_name();
}
}
Here is a test module that it would load when invoking $this->module->load_module('test'); and it creates the object outputting the test strings via echo statements. Heres the code for the test module that was constructed. Which should cause no problems as there is not really a solution but just an output string, but posted any way.
<?php
class testController {
function __construct() {
echo 'test controller from modules <br />';
}
}
However when running the page i am getting some errors can any one help out?
Notice: Undefined variable: test in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
Fatal error: Cannot access empty property in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
I have a small problem with the following php code. On the input for the name, I'm trying to show their current username, by $user->username, only it gives me the following error:
Notice: Undefined variable: user in /home//domains//public_html/dev/edit_account.php on line 36
Notice: Trying to get property of non-object in /home//domains//public_html/dev/edit_account.php on line 36
in the game_header.php file I have
$user = new User($_SESSION['id']);
And thought it would work, but sadly it does not.
I have also tried
$user = new User($_SESSION['id']);
on the edit_account.php page, but I was getting the same error.
Here's the edit_account.php code.
Does anyone know what I might be doing wrong here?
include "game_header.php";
$_GET['type'] = isset($_GET['type']) && ctype_alpha($_GET['type']) ? trim($_GET['type']) : '0';
switch($_GET['type']) {
case 'profileoptions' : profile_options(); break;
default : profile_options();
}
function profile_options() {
echo '
';
include 'game_footer.php';
}
When encased into a function you must do the following:
global $user ; //Bring a global variable to the current scope.
echo $user->username ; //Then you can access it and its properties.
So it must start with:
function profile_options() {
global $user ;
//The rest of code
}
However, I recommend you to create a parameter:
function profile_options(User $user){
//Much code
}
Then call it where $user is accessible:
profile_options($user) ;
The undefined constant question isn't a new one, and I've tried Google already and found a million answers involving arrays with missing quotes and that's not my solution. I'm learning OOP coding with PHP and this is literally my first few hours working with it. This is the class I created.
class template {
var $page;
function __construct() {
$this->page = 'home';
}
function getHeader($header = 'header') {
include ('template/'.$header.'.php');
}
function getFooter($footer = 'footer') {
include ('template/'.$footer.'.php');
}
function setPage($page = 'home') {
$this->page = $page;
}
function getPage() {
if(page === 'home') {
include('template/home.php');
} else {
include('template/'.$this->page.'.php');
}
}
}
And this is how I instantiated it.
include('class.template.php');
$template = new template();
$template->getHeader();
if(isset($_GET['page'])) {
$template->setPage($_GET['page']);
}
$template->getPage();
$template->getFooter();
And of course the error - Notice: Use of undefined constant page - assumed 'page' in /Applications/MAMP/htdocs/titan-up/class.template.php on line 24
This is obviously something I should spot right away that I'm doing wrong but it's late and I'm at a loss. Any help is greatly appreciated.
Edit: Any links that make the learning process easier would be greatly appreciated. I'm already going through the PHP manual on it and have a strong background in PHP, but not OOP.
Change from this:
function getPage() {
if(page === 'home') {
To this:
function getPage() {
if($this->page === 'home') {
The error message "Notice: Use of undefined constant page - assumed 'page'" isn't terribly helpful, but it's due to the unfortunate fact that PHP will implicitly convert an unknown token as a constant with the same value.
That is it's seeing page (which doesn't have a $ in front of it and therefore isn't a variable name) and treating it as though there was a previous statement define('page', 'page');.
Note: Another common cause of this error message is if you forget to wrap a string in quotes - eg $some_array[some_key] instead of $some_array['some_key'].