class not found if included from extern - php

I have a this folder structure:
+ inc
++ functions.php
++ vendor
+ private
++ carddav
+++ check.php
my check.php looks like this and works:
<?php
require '../../inc/functions.php';
// CardDAV API
require '../../inc/vendor/autoload.php';
use MStilkerich\CardDavClient\{Account, AddressbookCollection, Config};
use Psr\Log\{AbstractLogger, NullLogger, LogLevel};
use Sabre\VObject\Component\VCard;
class StdoutLogger extends AbstractLogger {
public function log($level, $message, array $context = array()) {
if ($level !== LogLevel::DEBUG) {
$ctx = empty($context) ? "" : json_encode($context);
echo ">>> ".$message . $ctx . "<br />";
}
}
}
Config::init(new StdoutLogger());
$account = new Account(URL, USERNAME, PASSWORD);
$abook = new AddressbookCollection(URL, $account);
$vcard = new VCard();
?>
But now I would like to outsource this part into my functions.php:
functions.php
<?
// CardDAV API
require 'vendor/autoload.php';
use MStilkerich\CardDavClient\{Account, AddressbookCollection, Config};
use Psr\Log\{AbstractLogger, NullLogger, LogLevel};
use Sabre\VObject\Component\VCard;
class StdoutLogger extends AbstractLogger {
public function log($level, $message, array $context = array()) {
if ($level !== LogLevel::DEBUG) {
$ctx = empty($context) ? "" : json_encode($context);
echo ">>> ".$message . $ctx . "<br />";
}
}
}
Config::init(new StdoutLogger());
$account = new Account(URL, USERNAME, PASSWORD);
$abook = new AddressbookCollection(URL, $account);
?>
check.php
<?php
require '../../inc/functions.php';
$vcard = new VCard();
?>
But now I get this error when I open the check.php:
Fatal error: Uncaught Error: Class 'VCard' not found in check.php:26
Where is my mistake?
Tank you !

Imports using use statement work on a per file basis. I.e. classes need to be imported in the file where they're used. In your case remove Sabre\VObject\Component\VCard import from functions.php and move it to check.php where VCard class is actually used.

Related

class not found even it's included PHP

the path also correct but i still don't know why it shows class not found as a fatal error
<?php
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use core\Http;
if(class_exists("Response")){
echo "ok";
}
else {
echo "doesn't exist";
}
$r = new Response();
$r->header('test');
?>
normal call in php file,
<?php
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use Core\Http\Response;
if(class_exists( 'Core\Http\Response' )){
$r = new Response(); // -OR- $r = new Core\Http\Response();
}else{
die('class doesn\'t exist');
}
$r->header('test');
?>
If you want to call in any controller, call like following
<?php
// Start of the file
ini_set('display_errors', 1);
require_once('../core/Http/Response.php');
use Core\Http\Response;
// your class
class Test extends Test_Controller {
public function test(){
if(class_exists( 'Core\Http\Response' )){
$r = new Response(); // -OR- $r = new Core\Http\Response();
}else{
die('class doesn\'t exist');
}
$r->header('test');
}
}
?>
<?php
ini_set('display_errors', 1);
require_once dirname(__DIR__) . '/Core/Http/Response.php';
use \Core\Http\Response;
$test = new Response("test response");
echo $test ;
?>

PHP vTiger6 Issue creating account with vtwsclib

I am trying to figure out how to programmatically create a "Account" in vTiger6.5 using its Web Services API.
I am currently using the official vtwsclib v1.5 PHP Library. The log in, appears to succeed and I can also successfully perform a doDescribe on the module, however doCreate returns 'false' no matter what I do.
Sample below. Am I missing anything?
$url = 'http://vtiger.mydomain.com/';
$client = new Vtiger_WSClient($url);
$login = $client -> doLogin('systemuser', 'O8nFgnotrealkey');
if (!$login)
echo 'Login Failed';
else {
$module = "Accounts";
$record = $client -> doCreate($module, Array('accountname'=>'S1',
'account_type'=>'Prospect', 'phone'=>'75584'));
if ($record) {
$recordid = $client -> getRecordId($record['id']);
}
}
Returns:
$record: bool(false)
With $client->lastError() i now what my error ,I have create same mandatory fields
$module = 'Accounts';
$record = $client->doCreate($module, Array('accountname'=>'c2', 'account_type'=>'Prospect', 'industry'=>'444', 'phone'=>'4555',cf_753=>'Code',cf_755=>'45588','assigned_user_id' => '14x4'));
if($record) {
echo "done";
//$recordid = $client->getRecordId($record['id']);
//echo "Record ".$recordid;
}
else {
$wasError= $client->lastError();
if($wasError) {
echo "</br>last error : ".$wasError['code'] . ':' . $wasError['message'];
}
}

How to do a catch all in Slim framework?

I'm making a short URL service using Slimphp to take care of my routing. I can define any route just fine but if I want to react to /<code here> instead of that taking me to the index page of the project.
This is my code:
<?php
require 'vendor/autoload.php';
use ShortUrls\ShortUrls;
error_reporting(E_ALL);
ini_set('display_errors', true);
$app = new \Slim\Slim();(array(
"view" => new \Slim\Views\Smarty()
));
$view = $app->view();
$view->parserDirectory = dirname(__FILE__) . 'vendor/smarty/smarty/libs';
$view->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$view->parserCacheDirectory = dirname(__FILE__) . '/cache';
$view->setTemplatesDirectory(dirname(__FILE__) . '/lib/templates/');
\ShortUrls\Config::init_config();
$app->get('/', function ($hash) {
try {
} catch (ResourceNotFoundException $e) {
echo '404';
}
$short = new ShortUrls();
if ($hash) {
if ($short_url = $short->get_url_by_hash(($hash))) {
print '<pre>';
print_r($short_url);
print '</prE>';
}
} else {
$short->create_short_url("http://www.locovsworld.com");
// $app->render('layout.tpl', array('test' => 'Hello'));
}
global $app;
print_r( $app->request()->params() );
echo 'done';
});
$app->run();
Remember / == index /9082ABC could be a short URL that I have to query from the database and redirect the client to.
I already got the answer its the following ...
$app->get('/(:hash)', function ($hash) {
};
I am sorry to bother you guys :(

PHP Fatal error: Class 'Slim' not found

session_start();
date_default_timezone_set('GMT');
require 'Slim/Slim.php';
use Slim\Slim;
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
require_once 'item.php';
this is code excerpt from index.php and stuck on the said error when it called item.php. Here the contains of the file
$app->put('/getItem', authorize(), 'getItem');
function getItem() {
$sql = "SELECT * FROM item";
$app = Slim::getInstance();
try {
$db = getConnection();
$stmt = $db->query($sql);
$item = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response = $app->response();
$response->header('Content-Type', 'application/json');
// Include support for JSONP requests
if (!isset($_GET['callback'])) {
echo json_encode($item);
} else {
echo $_GET['callback'] . '(' . json_encode($item) . ');';
}
} catch(PDOException $e) {
$error = array("error"=> array("text"=>$e->getMessage()));
echo json_encode($error);
}
}
i hit the error on this $app = Slim::getInstance();
What is wrong with my approach?
The Slim class' full name (including namespace) is \Slim\Slim so you'll need to use that, eg
$app = \Slim\Slim::getInstance();
Alternatively, you can import the Slim symbol using the use statement at the top of your item.php script.
use Slim\Slim;
You can use this code for Slim Framework3:
<?php
require "vendor/autoload.php";
use \Slim\App;
$app = new App();
$app->get("/",function(){
echo "Hello World";
});
$app->get("/test",function(){
echo "Hello World";
});
$app->run();
?>

OWL Reader::read () error

Im trying to send my search variable to ontology and see if it does belong to any class or subclass as their property. how can i do that?
<?php
$search=$_GET['searchtext'];
if(empty($search)){
echo ("You don't enter anything.");
}
else{
echo ("String you enter is " . $search . " !");
}
require_once "owllib/OWLLib.php";
require_once "$OWLLIB_ROOT/reader/OWLReader.php";
require_once "$OWLLIB_ROOT/memory/OWLMemoryOntology.php";
$reader = new OWLReader();
$ontology = new OWLMemoryOntology();
$reader->read($filename, $ontology);
$class = $ontology->getClass($search);
echo("Ontology output is " .$class);
?>

Categories