The url to JSON structure
http://api.giphy.com/v1/gifs/search?q=funny+cat&offset=100&limit=1&api_key=dc6zaTOxFJmzC
Now I'm trying to parse this Json data to my objects:
this is my class:
<?php
class MappedEntity{
private $id;
private $mp4Url;
public function setId($id){
$this->id=$id;
}
public function setMp4Url($mp4Url){
$this->mp4Url=$mp4Url;
}
public function getId(){
return $id;
}
public function getMp4Url(){
return $mp4Url;
}
function __contruct($jsonData){
foreach($jsonData as $key => $val){
if(property_exists(__CLASS__,$key)){
$this->$key = $val;
}
}
}
}
?>
the code thats calling the class:
<?php
require_once 'mappedEntity.php';
$keyword = $_POST["keyword"];
$url="http://api.giphy.com/v1/gifs/search?q=".$keyword."&offset=100&limit=1&api_key=dc6zaTOxFJmzC";
$json = file_get_contents($url);
$file = json_decode($json);
$entity = new MappedEntity($file);
echo $entity->getId();
?>
Im getting
Notice:
Undefined variable: id in C:...\MappedEntity.php on line 14
which is following ine
13 public function getId(){
14 return $id;
15 }
OK, I changed the getter methods, and constructor , now the error goes
Notice: Undefined variable: id in C:\....\MappedEntity.php on line 14
Fatal error: Cannot access empty property in C:...\MappedEntity.php on line 14
I assume , my mapping method inside of constructor is not working fine?
You're missing the $this on both getID and getMp4URL functions, they should read:
public function getId(){
return $this->id;
}
and
public function getMp4Url(){
return $this->mp4Url;
}
Related
I have 3 separate files, item.php, proposal.php and output.php - this is supposed to be a cart like application, the idea is to for the user to select an item and the item in to the Proposal class... however I am running in to the following error:
Fatal error: Uncaught Error: Cannot use object of type __PHP_Incomplete_Class as array in C:\xampp\htdocs\proposal.php:12 Stack trace: #0 C:\xampp\htdocs\output.php(9): Proposal->addItem(Object(Item)) #1 {main} thrown in C:\xampp\htdocs\proposal.php on line 12
I've searched around SO & Google, and tried various things including placing session_start() before the includes for item.php and proposal.php, however that did not solve the issue, the error just changed to:
Cannot use object of type Proposal as array
Any ideas? Running PHP 7.0.9
item.php
<?php
class Item {
protected $id;
protected $name;
protected $manufacturer;
protected $model;
protected $qty;
protected $serial;
public function __construct($id,$name,$manufacturer,$model,$qty,$serial) {
$this->id = $id;
$this->name = $name;
$this->manufacturer = $manufacturer;
$this->model = $model;
$this->qty = $qty;
$this->serial = $serial;
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getManufacturer() {
return $this->manufacturer;
}
public function getModel() {
return $this->model;
}
public function getQty() {
return $this->qty;
}
public function getSerial() {
return $this->serial;
}
}
proposal.php
class Proposal {
protected $items = array();
public function __construct() {
$this->items = isset($_SESSION['proposal']) ? $_SESSION['proposal'] : array();
}
public function addItem(Item $item) {
$id = $item->getId();
// the following line is line 12 of proposal.php
if(isset($this->items[$id])) {
$this->items[$id]['qty'] = $this->items[$id]['qty'] + $item->getQty();
}
else {
$this->items[$id] = $item;
}
}
}
output.php
session_start();
include('item.php');
include('proposal.php');
$item = new Item($_GET['id'],$_GET['name'],$_GET['manufacturer'],$_GET['model'],$_GET['qty'],$_GET['serial']);
$proposal = new Proposal();
$proposal->addItem($item);
$_SESSION['proposal'] = $proposal;
// view output in array/object format if session variable set
if(isset($_SESSION['proposal'])) { print '<pre>' . print_r($_SESSION['proposal'],1) . '</pre>'; }
EDIT: I believe this issue may be session related because the error does not appear until the 2nd run.
Output on first run is:
Proposal Object
(
[items:protected] => Array
(
[25] => Item Object
(
[id:protected] => 25
[name:protected] => Computer
[manufacturer:protected] => Dell
[model:protected] => Alienware
[qty:protected] => 11
[serial:protected] => 12345678
)
)
)
session_start();
include('item.php');
include('proposal.php');
Your session is initialized before classes have been declared.
This is leading to __PHP_Incomplete_Class.
Problem with "Cannot use object of type Proposal as array":
public function __construct() {
$this->items = isset($_SESSION['proposal']) ? $_SESSION['proposal'] : array();
}
If your session contains key proposal, you are using it as storage variable, but this is initialized as instance of Proposal in output.php:
$proposal = new Proposal();
$proposal->addItem($item);
$_SESSION['proposal'] = $proposal;
One way to avoid this is to create a session singleton of Proposal:
class Proposal {
protected function __construct() {}
public static function getInstance()
{
if (!isset($_SESSION['proposal'])) {
$_SESSION['proposal'] = new Proposal;
}
return $_SESSION['proposal'];
}
}
Hello i'm having trouble with this class when it comes to multidimensional arrays and setting them and i cannot understand why is behaving like this...
Anyone who can help me fix it?
Few Usage Examples:
echo $config->session->gcDivisor;
echo $config['session']['gcDivisor'];
$config['logged'] = '1';
$config->logged = '1';
$config['user']['logged'] = '1';
$config->user->logged = '1';
Errors for each of the lines
Notice: Trying to get property of non-object in /home/biroexpert/subdomains/prod/index.php on line 33
200
Notice: Undefined index: user in /home/biroexpert/subdomains/prod/Library/Twelve/Config/Config.php on line 67
Notice: Indirect modification of overloaded element of Twelve\Config\Config has no effect in /home/biroexpert/subdomains/prod/index.php on line 37
Notice: Undefined index: user in /home//subdomains/prod/Library/Twelve/Config/Config.php on line 87
Notice: Indirect modification of overloaded property Twelve\Config\Config::$user has no effect in /home//subdomains/prod/index.php on line 38
Strict Standards: Creating default object from empty value in /home//subdomains/prod/index.php on line 38
Code
<?php
namespace Twelve\Config;
use \ArrayObject;
use \Twelve\SingletonPattern\LazySingleton;
use \Twelve\SingletonPattern\Interfaces\ILazySingleton;
/**
* Singleton With Configuration Info
*/
class Config extends ArrayObject implements ILazySingleton
{
/**
* Stores FileName
* #var Config
*/
protected static $_configFile = '';
/**
* Config Settings Array
* #var Config
*/
protected $_settings = array();
public static function getInstance(){
return LazySingleton::getInstance(__CLASS__);
}
/**
* Set the Config File Path
*/
public static function setFile($filePath)
{
static::$_configFile = $filePath;
}
public function __construct()
{
LazySingleton::validate(__CLASS__);
$values = include(static::$_configFile);
if(is_array($values))
{
// Allow accessing properties as either array keys or object properties:
$this->_settings = new ArrayObject($values, ArrayObject::ARRAY_AS_PROPS);
}
}
// some debug method
public function dumpArray()
{
echo "<pre>";
print_r($this->_settings);
}
public function getIterator()
{
return $this->_settings->getIterator();
}
public function offsetExists($index)
{
return $this->_settings->offsetExists($index);
}
public function offsetGet($index)
{
return $this->_settings->offsetGet($index);
}
public function offsetSet($key, $value)
{
return $this->_settings->offsetSet($key, $value);
}
public function offsetUnset($index)
{
return $this->_settings->offsetUnset($index);
}
public function count()
{
return $this->_settings->count();
}
/**
* Prevent Cloning
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
// No Cloning Allowed
}
}
This is my code:
<?php
class VortexORM {
private static $orm = null;
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public static function getInstance() {
if (VortexORM::$orm == null)
VortexORM::$orm = new VortexORM();
return VortexORM::$orm;
}
public static function set($name, $value) {
$orm = VortexORM::getInstance();
//echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]";
$orm->$name = $value;
}
public static function get($name) {
$orm = VortexORM::getInstance();
// echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]";
return $orm->$name;
}
}
To get data I use:
var_dump(VortexORM::get('admin_links'));
var_dump(VortexORM::get('admin'));
To set data I use:
VortexORM::set('admin_links',array(....));
However, I get the following warnings:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin_links
Filename: Vortex/VortexORM.php
Line Number: 8
NULL
A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin
Filename: Vortex/VortexORM.php
Line Number: 8
Why am I getting these warnings?
I want to be able to access it like this in CodeIgniter as a static function:
$this->vortexorm->admin_links = array(....);
OK, I just tested this code:
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class VortexORM {
private static $orm = null;
public function __get($name) {
return $this->$name;
}
public function __set($name, $value) {
$this->$name = $value;
}
public static function getInstance() {
if (VortexORM::$orm == null)
VortexORM::$orm = new VortexORM();
return VortexORM::$orm;
}
public static function set($name, $value) {
$orm = VortexORM::getInstance();
//echo "Setting [ <b>{$name}</b> :: <i>{$value}</i>]";
$orm->$name = $value;
}
public static function get($name) {
$orm = VortexORM::getInstance();
// echo "Getting [ <b>{$name}</b> :: <i>{$orm->$name}</i>]";
return $orm->$name;
}
}
VortexORM::set('admin_links',"test");
var_dump(VortexORM::get('admin_links'));
It just worked fine for me. giving the following output:
string(4) "test"
So, I guess, you were might just trying to retrieve a property before its set? Or please share more details. Also, why are you trying to create new ORM, where we can use doctrine with codeigniter easily?
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 building some classes for working with playing cards. I have a Card class and a Deck class. I want to implement drawing a card from the deck by using array_shift() on an array of Card objects; this array is a property of Deck. Here is the code for the classes, which is stored in the file "cardlib.php":
<?php
class Card
{
private $suit="";
private $face="";
function __construct($suit,$face){
$this->suit=$suit;
$this->face=$face;
}
public function getSuit(){
return $suit;
}
public function getFace(){
return $face;
}
public function display(){
echo $this->suit.$this->face;
}
}
class Deck
{
private $suits=array("S","H","C","D");
private $faces=array("2","3","4","5",
"6","7","8","9","10",
"J","Q","K","A");
private $stack=array();
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$stack[] = $card;
}
}
}
public function doShuffle(){
shuffle($this->stack);
}
public function draw(){
$card = array_shift($this->stack);
var_dump($card);
return $card;
}
}
?>
And here is the test code, in "index.php":
<?php
include_once "cardlib.php";
$myDeck=new Deck();
$myDeck->doshuffle();
$card=$myDeck->draw();
$card->display();
?>
The test code gives me the following error message:
NULL
Fatal error: Call to a member function display() on a non-object in C:\wamp\www\cardgames\index.php on line 6
It seems that array_shift() isn't returning the reference to the card object, or I'm not properly initializing the $card variable with what array_shift() returns. How do I get the object that I want?
In the constructor, you store the stack in a local variable. Use $this->stack to store it in the member variable.
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$this->stack[] = $card;
}
}
}
In Deck::__construct(), use $this->stack[] = .. instead of $stack[] = ..