Call to a member function getDOM() on a non-object - php

I'm trying to create a PHP function that adds an event to a google Calendar. It appears to be building the object correctly but it throws a "Call to a member function getDOM() on a non-object in FeedEntryParent.php" error when trying to add the event. Here is the abbreviated class with only the constructor and the function that adds the event:
class GCal_datasource {
private $user = 'xxxxxxx';
private $pass = 'xxxxxxxxx';
private $client;
private $gdata_cal;
private $calendar;
private $visibility;
public function __construct($settings = NULL){
session_start();
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
if($settings == NULL){
$settings = array();
$settings['calendar'] = 'default';
$settings['visibility'] = 'full';
}
$this->calendar = $settings['calendar'];
$this->visibility = $settings['visibility'];
$this->client = $this->get_ClientLoginHttpClient();
$this->gdata_cal = new Zend_Gdata_Calendar($this->client);
}
public function create_event($fields){
$gc = $this->gdata_cal;
$new_event = $this->gdata_cal->newEventEntry();
echo '<pre>';
print_r($fields);
echo '</pre>';
if(isset($fields['quick_add'])){
$new_event->content = $gc->newContent($fields['quick_add']);
$new_event->quickAdd = $gc->newQuickAdd(true);
} else if (isset($fields['title']) && isset($fields['when'])) {
$new_event->title = $fields['title'];
$where = $gc->newWhere($fields['where']);
$new_event->where = array($where);
$desc = $gc->newContent($fields['desc']);
$new_event->content = $desc;
$new_event->content->type = 'text';
$new_event->when = self::build_when_array($fields['when']);
if(isset($fields['web_content'])){
$new_event->link = web_event_array($fields['web_content']);
}
}
echo '<pre>';
print_r($new_event);
echo '</pre>';
$gc->insertEvent($new_event);
return $created_event->id->text;
}
}
The error (I believe) is how I am calling insertEvent() towards the end of the code. The only reason I think that is I only get the error when it exists and if I remove it the echo above it prints out the Event object as intended.
Anyone with a better grasp of the Goggle PHP API that can lend me a hand I would greatly appreciate.

I had this problem.
I think you have to change string
$new_event->title = $fields['title'];
to
$new_event->title = $service->newTitle($fields['title']);

Call to a member function getDOM() on a non-object in FeedEntryParent.php means that somewhere in the file named "FeedEntryParent.php" you have called getDOM() on a variable and that variable is not an object and so does not have a getDOM() method.
Nowhere in the code you posted is a call to getDOM(), so the error is not generated in the posted code.
Track down that call to getDOM(). The error usually gives you a line number. See what variable you are calling the method on. That variable is not an object. Find where that variable is set - that's probably where your problem is.

Related

How best to Access Php Static Variables from another Php file

I have a static variable like so to keep track of some operations in memory without using a real database
<?php
class Database
{
public static $database = array();
}
When I try to access the database from another php file like so
<?php
include 'database.php';
function createPaymentRef($username, $password, $amount)
{
$finalResult = array();
$ref = generateTimedTransactionRef($username, $password, $amount);
global $requestData;
global $ip;
Database::$database->unset($username); //Expected type 'object'. Found 'array'.intelephense(1006) error
$requestData->ip = $ip;
$requestData->reference = $ref;
$finalResult['result'] = $ref;
return $finalResult;
}
?>
I am get an error saying
Expected type 'object'. Found 'array'.intelephense(1006)
How can I resolve this?
-> is used to access properties or methods of an object. Database::$database is an array, not an object. The syntax to unset an array element is
unset($array[$index])
so it should be
unset(Database:$database[$username]);
Check if the static variable exists and if the array element in it exists, then unset using unset(Database::$database[$username]):
$db = Database::$database;
if($username && $db && isset($db[$username])){
unset(Database::$database[$username]);
}

Fatal error: Cannot use [] for reading

I'm getting this error
Fatal error: Cannot use [] for reading in... on line 26
Checking all the threads that have been made here on this error, I still cannot figure it out. Looking at my code there's nothing I'm doing wrong.
<?php
class Person
{
//Variables for personal information//
private $navn;
private $adresse;
private $postnummer;
private $poststed;
private $telefonnummer;
private $fodselsdato;
public function __construct($navn, $adresse, $postnummer, $poststed, $telefonnummer, $fodselsdato)
{
$this->navn = $navn;
$this->adresse = $adresse;
$this->postnummer = $postnummer;
$this->poststed = $poststed;
$this->telefonnummer = $telefonnummer;
$this->fodselsdato = $fodselsdato;
}
//Creates an array to store education for a person//
private $utdanning = array();
//Function to add education to the array//
public function leggTilUtdanning(Utdanning $utdanning)
{
$this->utdanning[] = $utdanning;
}
}
//Class for education
class Utdanning
{
private $institusjon;
private $studieretning;
private $grad;
private $startet;
private $ferdig;
public function __construct($institusjon, $studieretning, $grad, $startet, $ferdig)
{
$this->institusjon = $institusjon;
$this->studieretning = $studieretning;
$this->grad = $grad;
$this->startet = $startet;
$this->ferdig = $ferdig;
}
}
$person1 = new Person('Dave Lewis', 'Downing Street 14', 0442, 'Northville', 98765432, '17.05.1975');
$utdanning = new Utdanning('Harvard', 'Economics', 'Bachelor', 2013, 2016);
$person1->leggTilUtdanning($utdanning);
?>
The error comes from the line inside the function where I'm trying to add an Utdanning-object to the array. It's funny, cause I tried this very same method of doing it on another project, using the exact same syntax, and it worked perfectly. Furthermore, I don't understand why it says I'm trying to read from the array, when I'm actually adding to it.
Does anyone have an idea what's going on here?
EDIT: I circled the problem and made a more simple version of the code so you can see for yourself.
So, just to mark this solved, I got rid of the problem simply by rewriting the characters inside the method leggtilUtdanning. Appears to have been some sort of character encoding-problem like you pointed out, but I have absolutely no idea how that happened. Anyway, thanks for all the help.

PHP: Load a class in a class

I have a class which is meant to "load" an another class, however I haven't been able to get it to work.
Error Message
Fatal error: Call to undefined method stdClass::echoString() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\classes\example.php on line 5
Code
My code is broken up into three main sections:
api.php - the class to load the other classes.
API/exampleExternalAPI.php - (multiple files) the classes that api.php loads
example.php - the file that uses the main class (api.php)
If it helps these files can be downloaded from my dropbox
api.php
<?php
/* Config */
define('pathToAPIs','API/');
/* Autoload Function */
spl_autoload_register(function($className){
$namespace=str_replace("\\","/",__NAMESPACE__);
$className=str_replace("\\","/",$className);
$class=pathToAPIs.(empty($namespace)?"":$namespace."/")."{$className}.php";
include_once($class);
});
class api {
private $listOfAPIs;
public $APIs;
public function __construct($setAPI = null){
$this->updateListOfAPIs();
if (isset($setAPI)){
return $this->setAPI($setAPI);
}
}
public function setAPIs($setAPIs){
$this->APIs = null; // clears a previous call to this method
if (!is_array($setAPIs)){ // if not an array
$setAPIs = array($setAPIs); // make array
}
foreach ($setAPIs as $setAPIType){
if(in_array($setAPIType,$this->listOfAPIs)){
$array[$setAPIType] = new $setAPIType;
}
}
$this->APIs = json_decode(json_encode($array), FALSE); // convert array of required api objects to an object
return $this->APIs;
}
public function getListOfAPIs($update = false){
if ($update){
$this->updateListOfAPIs();
}
return $this->listOfAPIs;
}
private function updateListOfAPIs(){
$this->listOfAPIs = null; // clears a previous call to this method
$it = new FilesystemIterator(pathToAPIs);
foreach ($it as $fileinfo){
$filename = pathinfo($fileinfo->getFilename(), PATHINFO_FILENAME); // removes extension
$this->listOfAPIs[]= $filename;
}
}
public function __call($method,$args){
}
}
API/exampleExternalAPI.php
<?php
class exampleExternalAPI {
public function echoString($string){
echo $string;
}
}
example.php
<?php
require_once 'api.php';
$api = new api();
$api->setAPIs('exampleExternalAPI');
$api->APIs->exampleExternalAPI->echoString('string');
Background Info
(may give some insight to my madness)
I'm working on a project where I need to connect to lots of external APIs.
So I decided to creating a class to look after all my communications with external APIs ( not sure if best way - new to Object Oriented Programming).
I'm not entirely sure what problem you're trying to solve, but if your APIs is a simple stdClass instance it should work as expected:
public function setAPIs($setAPIs)
{
$this->APIs = new stdClass; // clears a previous call to this method
if (!is_array($setAPIs)) { // if not an array
$setAPIs = array($setAPIs); // make array
}
foreach ($setAPIs as $setAPIType) {
if (in_array($setAPIType, $this->listOfAPIs)) {
$this->APIs->{$setAPIType} = new $setAPIType;
}
}
return $this->APIs;
}

Accessing array in class structure

I want to validate a form with php.
Therefor I created a class "benutzer" and a public function "benutzerEintragen" of this class to validate the form:
class benutzer
{
private $username = "";
private $mail = "";
private $mail_check = "";
private $passwort = "";
private $passwort_check = "";
public $error_blank = array();
public $error_notSelected = array();
public $error_notEqual = array();
public function benutzerEintragen () {
$textfields[0] = $this->username;
$textfields[1] = $this->mail;
$textfields[2] = $this->mail_check;
$textfields[3] = $this->passwort;
$textfields[4] = $this->passwort_check;
foreach ($textfields as $string) {
$result = checkFormular::emptyVariable($string);
$error_blank[] = $result;
}
In the function "benutzerEintragen" i filled the variables "username,mail" and so on with the appropriate $_POST entries (not shown in the code above). The call
checkFormular::emptyVariable($string)
just returns "TRUE" if the field is not set or empty otherwise FALSE.
Now when i try to create a new instance of this class, execute the function and get access to $error_blank[0] the array is empty!
if (($_SERVER['REQUEST_METHOD'] == 'POST')){
$BENUTZER = new benutzer();
$BENUTZER->benutzerEintragen();
echo $BENUTZER->error_blank[0];}
So the last line is leading to a "Notice: Undefined offset: 0". It seems to be related to the array structure, because if i do
echo $BENUTZER->mail;
I get any input I wrote in the form, which is correct. Also the foreach loop seems to do the right thing when i run the debugger in phpEd, but it seems like the array "error_blank" is erased after the function is executed.
Any help would be greatly appreciated. Thanks in advance.
There is a scope problem here. You do have a class attribute with the name. Unlike in Java where using a local variable with the same name as a class variable automatically selects the class attribute this is not the case in PHP.
Basically you are saving your output in a local variable which gets discarded once you leave the function. Change $error_blank[] = $result; to $this->error_blank[] = $result; and you should be fine.
First of all this seems overly complicated way to do a simple task, but that wasn't actually the question.
You are creating a new $error_blank variable that is only in function scope. If you wish to use the class variable you should use $this->error_blank[]

Fatal error: Call to undefined method weather::get()

I was trying to make weather based PHP script to show weather related data.But,I'm facing this error message
Fatal error: Call to undefined method weather::get()
Would you please let me know how can i fix this or what was my problem??You can check out my code here:
<?php
include 'weather.php';
$t_weather = new weather();
$info = $t_weather->get('New York');
echo "Current temperature in {$info[0]['location']} is: {$info[0]['current_condition']['temperature']['f']} °F";
?>
This is weather.php:
<?php
class weather {
// API data
private $API_NAME = 'weather';
private $API_KEY = '***********';
}
?>
Thanks in advance.
Well your weather class doesn't have a method named get. Are you using someone else's class to do this? You should have something like:
class weather {
// API data
private $API_NAME = 'weather';
private $API_KEY = '***********';
public function get($location) {
// code that gets the weather for $location
}
}

Categories