I've got this weird error.
I've got DB class and Live class. the Live class is not extends DB class.
I can't understand why when i declare the New Live this happened.
The Errors is:
Notice: Undefined property: Live::$isOff in /home/main/Database/DB.class.php on line 570
Fatal error: Call to undefined method Live::Query() in /home/main/Database/DB.class.php on line 574
This is what i got on this line:
ON DB.Class.php
public function setCharset($charset) {
if (self::$isOff){
return false;
}
self::$charset = $charset;
self::Query("SET NAMES '".$charset."'");
return self;
}
ON Live.Class.php
class Live
{
protected $PaypalLogo = '';
protected $isQA = null;
protected $siteURL = null;
public function __construct() {
DB::setCharset("utf8");//Setting charset here and that is gives Fatal Error.
if (strstr($_SERVER['SERVER_NAME'], "ba.")) {
$this->isQA = true;
$this->siteURL = "http://ba.live.com";
}
else {
$this->isQA = false;
$this->siteURL = "http://www.live.com/";
}
}
Thank you for any advice.
I found the Problem. it was because there are no instance of DB and because of this, it was take the instance of Live .
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 the following error!
Fatal error: Constant expression contains invalid operations in >/PATH/initClass.php on line 5
For the code:
<?php
Class init
{
public const THEME = "aman/dev/frontend/";
private $root = dirname(__dir__)."/aman/dev/fontend/";
public function getFile($name,$value)
{
list(
$title
) = $value;
}
}
?>
I can't seem to figure out what's is happening.
Help would be appreciated.
Your problem is that you are using a function operation to set a value to a class variable. To fix your problem, use the following code (i.e. move initialization to the constructor)
<?php
Class init
{
public const THEME = "aman/dev/frontend/";
private $root;
public function __construct() {
$this->root = dirname(__dir__)."/aman/dev/fontend/";
}
public function getFile($name,$value)
{
list(
$title
) = $value;
}
}
?>
I have two classes: The first class - Database - handles all the database actions, i.e., insert, update, delete. The other handles class specific actions for the user. The user class extends the database class. The User class has all of the properties in it and am trying to get the methods from the Database class to perform actions on the properties from the User class. So I'm instantiating User in test.php:
<?php
require_once("user.php");
$user = new User();
$user->auth("Scott", "rascal");
echo $user->username;
?>
<html>
<head>
<title>test</title>
</head>
<body>
</body>
</html>
and I'm getting these errors:
Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Notice: Undefined property: Database::$tableName in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 83
Notice: Undefined property: Database::$id in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 85
Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Here's the user class:
<?php
require_once("db.php");
class User extends Database{
public $dbFields = array('username', 'password');
public $tableName = "users";
public $id;
public $username;
public $password;
public function auth($user, $pass){
$this->username = $user;
$this->password = $pass;
}
}
?>
And here's the section of the database class that's giving me trouble:
<?php
require_once("../config/main.php");
class Database{
public $db;
public function __construct() {
$this->connect();
}
public function connect(){
try {
$this->db = new PDO("mysql:host=".DB_SERVER."; dbname=".DB_NAME, DB_USER, DB_PASS);
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function properties() {
$properties = array();
foreach ($this->dbFields as $field) {
if (isset($this->field) || property_exists($this, $field)) {
$properties[$field] = $this->$field;
}
}
return $properties;
}
I had everything working fine until I tried to extend the class and move the properties to the child class. How do I get past the errors and achieve this correctly?
What you did seems like it should work, just make sure to construct a User object and not a Database object directly. This is the exact point of class inheritance (having a parent run functions and reference properties that you don't have to code in every subclass).
That said, maybe try defining $dbFields as an empty array in your Database class (and your other subclass properties for that matter). Not sure if it would fix it (I usually do the late-static-binding thing), but worth a try.
In your parent class declare variables as static and if they exist in the child class, they will be used in the parent class. Check out Late Static Binding (LSB)
You try to access dbFields in your Database class but it is declared in your User class.
IMO, you should declare the dbFields in your Database class & you can set this property in the User constructor.
Hey I just had the same problem but I could figure this out using late static binding here is my example some of the codes are extra from my own just focus on the show_fields() function
<?php
class x {
protected static $table_name;
public static $fields = array();
public function __construct() {
self::set_table_name();
}
public function called_class() {
return __CLASS__;
}
public function set_table_name() {
self::$table_name = static::called_class();
}
public function test() {
echo self::$table_name;
}
public function show_fields() {
echo "<pre>";
print_r(static::$fields);
echo "</pre>";
echo "<br />";
foreach (static::$fields as $key => $value) {
if(property_exists(static::$table_name, $value)) {
echo static::$$value . "<br />";
}
}
}
}
class y extends x {
public static $fields = array('id','title');
public static $id = 'new id';
public static $title = 'new title';
public function called_class() {
return __CLASS__;
}
}
$xx = new x();
$yy = new y();
$yy->test();
$yy->show_fields();
?>
this is my first question
I Have following class
class ProDetection {
public function ProDetection ( ) { }
public function detect($word) {
............
}
public function getScore() {
return $score;
}
}
class SaveDetection {
public function SaveDetection($words) {
$proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
}
in other PHP file, i'm try to calling SaveDetection to getScore();
$konten = "xxxx xxxx xxx";
$save = new SaveDetection($konten);
print_r( $save->getScore() );
But i got an error message
Notice: Undefined property: SaveDetection::$proDetection in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Fatal error: Call to a member function getScore() on a non-object in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Please need your help
You never declare the the $proDetection member variable. Basically in your SaveDetection constructor you are declaring $proDetection as a local variable
class SaveDetection {
public function SaveDetection($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
EDIT:
PS. You should really use PHP's __construct() syntax instead of the old style of constructors. See here.
class SaveDetection {
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
Try this. Declare your variables as private (Coz if your code grows, it is hard to find what all the object or variables are used in the class.)
class SaveDetection {
private $proDetection = null;
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
I'm working on a model that tracks user data and stores it in a session, where appropriate. Here's the basic structure of it:
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('session');
if (!is_null($this->session->userdata('user'))) {
$arrData = $this->session->userdata('user');
$this->_netID = $arrData['_netID'];
$this->_fName = $arrData['_fName'];
$this->_eventMgr = $arrData['_eventMgr'];
$this->_accessMgr = $arrData['_accessMgr'];
$this->_accessAcnt = $arrData['_accessAcnt'];
$this->_accessEvnt = $arrData['_accessEvnt'];
$this->_accessCMS = $arrData['_accessCMS'];
$this->_accessReg = $arrData['_accessReg'];
$this->_accessRep = $arrData['_accessRep'];
$this->_accessPay = $arrData['_accessPay'];
$this->_okEvents = $arrData['_okEvents'];
}
}
public function __get($name) {
switch($name) {
default:
if (function_exists('parent::__get')) {
return parent::__get($name);
} else {
return $this->$name;
}
}
}
public function __set($name,$val) {
switch($name) {
default:
if (function_exists('parent::__set')) {
return parent::__set($name,$val);
} else {
$this->$name = $val; }
}
}
The issue I'm having is right in the constructor. Whenever it hits the seventh line (checking if the user key is null) it errors out, saying:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: SessionUser::$session
Filename: models/sessionuser.php
Line Number: 83
Fatal error: Call to a member function userdata() on a non-object in /usr/cwis/data/www-data/melioraweekenddev/system/application/models/sessionuser.php on line 38
Any ideas on why?
My guess is you're trying to load a library from within a model which is not directly possible.
Try instead:
public function __construct() {
parent::__construct();
$CI =& get_instance(); //Loads the codeigniter base instance (The object your controller is extended from. & for php4 compatibility
$CI->load->database();
$CI->load->library('session');
if (!is_null($CI->session->userdata('user'))) {
...