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.
Related
I have a simple PHP structure.
In admin_keys.php i have:
<?php
class admin_keys
{
public $key;
private $arr_keys = array("1", "2", "3");
public function check_key_admin()
{
if(in_array($this->key,$this->$arr_keys))
{
return true;
}else
{
return false;
}
}
}
?>
In ok.php i have:
<?php
include_once '../../all_keys.php';
$admin_keys = new admin_keys();
$admin_keys->key = "vailozluon";
$_isAdmin = $admin_keys->check_key();
if( _isAdmin== false)
{
echo 'deo phai';
}else { echo 'ok';}
?>
Im new so just bear with me
I don't know why this is return Uncaught Error: Call to undefined method admin_keys::check_key
any help will be appreciated.
thanks!
you are refering to function (i.e. check_key()) that does not exists in your class admin_keys.
You have function named check_admin_key() but you are trying to access it as check_key() which does not exist apparently throwing an error. So just change the name of the function being called as
$_isAdmin = $admin_keys->check_admin_key();
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 have coded my own PHP auto class loader but I receive the following error when I try to use the class functions with the class
Fatal error: Call to a member function Test() on a non-object
I would also like to know if this approach is the best approach available and if anyone suggests a better way of coding this then I will appreciate it.
$class = array();
foreach (scandir(include_dir) as $filename)
{
if (is_file(include_dir . '/' . $filename))
{
//its a php file, lets do this!
if (substr($filename, -4) == '.php')
{
$page = preg_replace('/\.php$/','',$filename);
$class[$page] = GetClass($page);
}
}
}
$class['Blue']->Test2();
$class['Blue2']->Test();
Iat seems that this error only occurs when there is numbers in the filename / class
And here is class loader file that i include in my index the class Blue works but Blue2 doesn't and throws that error.
function GetClass($class)
{
if (CheckAllOkay($class))
{
include('/blue/' . $class . '.php');
if (ClassCanBeAutoLoaded($class))
{
$newclass = new $class;
return $newclass;
}
}
}
function ClassCanBeAutoLoaded($class)
{
return class_exists($class);
}
function CheckAllOkay($class)
{
return file_exists($class);
}
Here is the class Blue2
<?php
class Blue2
{
function __construct()
{
echo '[LOADED]';
}
public function Test()
{
echo '[TEST CALLED]';
}
}
?>
Class Blue is the same is just echos a diffrent text and has a diffrent class name
Trying to get a hang of classes in php, trying to include carClass.php into new_file.php.
carClass.php
<?php
class carClass
{
private $color;
private $gear;
private $model;
private $gas;
function paintCar($carColor) {
$this->color = $carColor;
}
function findCarColor() {
echo "$color";
}
function shiftGear($newGear) {
$this->gear=$newGear;
}
function findGear() {
echo "$gear";
}
function chooseModel($newModel) {
$this->model = $newModel;
}
function findModel() {
echo"$model";
}
function fillCar($gasAmount) {
$this->gas = $gasAmount;
}
function lookAtGauge() {
echo "$gas";
}
}
?>
its just a bunch of getters and setters. Im trying to include this class to new_file.php
new_file.php
<?php
include("carClass.php");
$car = new carClass;
$car->chooseModel("Mustang");
$car->paintCar("black");
$car->shiftGear("5th");
$car->fillCar("half");
$car->findModel();
$car->findCarColor();
$car->findGear();
$car->lookAtGuage();
?>
When I try to execute this file I get these error messages
Warning: include(carClass.php): failed to open stream: No such file or directory in C:\xampp\htdocs\testFile\new_file.php on line 4
Warning: include(): Failed opening 'carClass.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\testFile\new_file.php on line 4
Fatal error: Class 'carClass' not found in C:\xampp\htdocs\testFile\new_file.php on line 6
I believe both files are in testFile directory so I'm not sure whats going on. I appreciate any help you guys can give me as usual.
The include path is set against the server configuration (PHP.ini) but the include path you specify is relative to that path so in your case the include path is (actual path in windows):
<?php
include_once dirname(__FILE__) . '/carClass.php';
$car = new carClass;
$car->chooseModel("Mustang");
$car->paintCar("black");
$car->shiftGear("5th");
$car->fillCar("half");
$car->findModel();
$car->findCarColor();
$car->findGear();
$car->lookAtGuage();
?>
You can use PHP's auto loading feature which will automatically load a class when an object is created. If you have many classes then you can use it an header file so that you don't need to worry about using this include every time.
<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
Try this:
<?php
class carClass
{
private $color;
private $gear;
private $model;
private $gas;
function paintCar($carColor) {
$this->color = $carColor;
}
function findCarColor() {
echo $this->color;
}
function shiftGear($newGear) {
$this->gear=$newGear;
}
function findGear() {
echo $this->gear;
}
function chooseModel($newModel) {
$this->model = $newModel;
}
function findModel() {
echo $this->model;
}
function fillCar($gasAmount) {
$this->gas = $gasAmount;
}
function lookAtGauge() {
echo $this->gas;
}
}
?>
//
<?php
include("carClass.php");
$car = new carClass;
$car->chooseModel("Mustang");
$car->paintCar("black");
$car->shiftGear("5th");
$car->fillCar("half");
$car->findModel();
$car->findCarColor();
$car->findGear();
$car->lookAtGauge();
?>
If I create a class that uses a variable defined in another php file that is included via require_once, I get the following outcomes:
If require_once is at the top of the class's php file and the variable is used in myclass->someFunction() it will throw the error: Undefined variable
If require_once is inside myclass->someFunction() it work once and thereafter throw the error: Undefined variable
How do I properly handle this?
Example showing the problem:
test.php
<?php
require_once( "holds_var.php" );
class T
{
function __construct()
{
$this->useVariable();
}
function useVariable()
{
echo $something;
}
}
$t = new T();
?>
holds_var.php
<?php $something = "I am something"; ?>
EXAMPLE 2 (uses the same "holds_var.php" ):
test.php
<?php
class T
{
function __construct()
{
//This is ok
$this->useVariable();
//This throws an error
$this->useVariable();
}
function useVariable()
{
require_once( "holds_var.php" );
echo $something;
}
}
$t = new T();
?>
Use global keyword:
function useVariable()
{
global $something;
require_once( "holds_var.php" );
echo $something;
}
Sounds like global could help your case.
http://us3.php.net/manual/en/language.variables.scope.php
holds_var.php
<?php
$something = "I am something";
global $something;
?>
test.php
<?php
require_once( "holds_var.php" );
class T
{
function __construct()
{
//This is ok
$this->useVariable();
//This throws an error
$this->useVariable();
}
function useVariable()
{
global $something;
echo $something;
}
}
$t = new T();
?>
How does the above code work out for you?