use of class with namespace error - php

in api.php i use severall classes, with autoloaders. in include them like this
use \protoware\cms\Account as Account;
use \protoware\cms\Content as Content;
use protoware\cms\Count as Count;
include __DIR__ . '/vendor/autoload.php';
Local this works fine, when i upload it on a server i get this
Europe/Brussels] PHP Fatal error: Uncaught Error: Class 'protoware\cms\Content' not found in /data/vhosts/achielvolckaert.be/wwwroot/api.php:97
Stack trace:
#0 {main}
thrown in /data/vhosts/achielvolckaert.be/wwwroot/api.php on line 97
line 97 is the first line where i open a connection:
$content = new protoware\cms\Content();
$data = $content->get_content('medisch');
I have 0 clue how to fix this, i updated autloader, checked if all files were uploaded...

Try add a \ before class name, like $content = new \protoware\cms\Content(); or just $content = new Content() since you have a use statement.
If this class is a dependency, check if its correctly installed (inside your vendor folder, i.e.). Maybe run composer install?

Related

MongoDB insertOne function not found

I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.

iLovePDF Library - Fatal error: Class 'Ilovepdf' not found

I was trying to compress PDF. I try my best to find out some library that can be free too for some limited purpose. I found out iLovePDF Library.
I try to get it done using composer and without composer too But not find any way to resolve it.
My Code:
<?php
require_once('vendor/autoload.php');
// require_once('vendor/ilovepdf/init.php');
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
$myTask = $ilovepdf->newTask('compress');
$file1 = $myTask->addFile('file1.pdf');
$myTask->execute();
$myTask->download();
?>
Fatal error: Uncaught Error: Class 'Ilovepdf' not found in C:\xampp\htdocs\PHP Doc\E15\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PHP Doc\E15\index.php on line 11
You need to use namespaces:
$ilovepdf = new \Ilovepdf\Ilovepdf('project_public_key','secret_key');
Or:
use \Ilovepdf\Ilovepdf;
$ilovepdf = new Ilovepdf('project_public_key','secret_key');
Thank You.
I resolved it by adding below line at the top of index.php
namespace Ilovepdf;

php - Class not found (composer.json)

I'm trying to implement a PHP Library for detecting CMS I found on github, but I can't for the life of me figure it out.
I am new to php and composer, but I installed composer in my directory, then updated the json as instructed, but it cannot find the classes, despite them all being in the directory.
running this code
include(__DIR__ . "/vendor/autoload.php");
$domain = "http://google.com";
$cms = new DetectCMS($domain);
if($cms->getResult()) {
echo "Detected CMS: ".$cms->getResult();
} else {
echo "CMS couldn't be detected";
}
gives me this error
PHP Fatal error: Uncaught Error: Class 'DetectCMS' not found in D:\Projects\Scraper\ccc.php:12
Stack trace:
#0 {main}
thrown in D:\Projects\Scraper\ccc.php on line 12
here is my file structure:
would anyone have any idea what I'm doing wrong with this information?
Thanks in advance
I think to include a class using the autoloader,in your class, you must do something about it:
in your composer.json
"autoload": {
"psr-4" :{"DetectCMS": "DetectCMS"}
}
after in your class DetectCMS.php:
<?php
use DetectCMS\DetectCMS;
or
<?php
$loader = require 'vendor/autoload.php';
$loader->add('DetectCMS', __DIR__.'DetectCMS');

Unknown parser class "PropelArrayParser"

I'm new to Propel and I need to work with a website that was already setup with Propel (1.6.9).
I got this working on my laptop and generated without any errors. (Yay!)
After generating classes (on the same schema.xml and other related files as earlier on) there seem to be some differences. (spotted in file sizes) When I uploaded these files to the web hosting where the old files were doing a fine job, I ran into an error:
Fatal error: Uncaught exception 'PropelException' with message 'Unknown parser class "PropelArrayParser"' in /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/parser/PropelParser.php:101
Stack trace:
#0 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/om/BaseObject.php(375): PropelParser::getParser('Array')
#1 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/om/BaseObject.php(424): BaseObject->exportTo('Array', 'fieldName')
#2 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/src/controllers/UsersController.php(26): BaseObject->__call('toArray', Array)
#3 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/src/controllers/UsersController.php(26): User->toArray('fieldName')
#4 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/framework/framework.php(196): require('/var/www/vhosts...')
#5 /var/www/vhosts/10/154462/web in /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/parser/PropelParser.php on line 101
The error is referring to:
$case = new AgencyCosts();
$case->fromArray($_POST, BasePeer::TYPE_FIELDNAME);
$case->save();
So I figured something was wrong with includes. That's why I added the following to my init.php:
set_include_path(dirname(DIR) . '/vendor/propel/propel1/runtime/lib/parser' . PATH_SEPARATOR . get_include_path());
require dirname(DIR) . '/vendor/propel/propel1/runtime/lib/parser/PropelJSONParser.php';
Without any success.
The init.php (where the including is done) is available here.
Any help would be appreciated.
You're getting this exception when Propel cannot find the class definition. From Propel's code:
// PropelParse::getParser
if (!class_exists($class)) {
throw new PropelException(sprintf('Unknown parser class "%s"', $class));
}
Propel generates a classmap file, where the paths of all Propel's classes are written. It seems like you just get the wrong file included or wrong paths in it. Take a look at your configuration file for the classmap key:
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-<you-project-name>-conf.php');
And then examine the contents of that file. It might happen because you blindly moved the files from your computer to the server.

SmartyBC (Backwards Compatibility Wrapper) doesn't work in PrestaShop

As adviced in Smarty's instruction, in order to use deprecated functions like include_php in newest PrestaShop, I had to switch from Smarty.class.php to SmartyBC.class.php. I did it by modyfing in smarty.config.inc.php following lines:
require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');
// require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
global $smarty;
// $smarty = new Smarty();
$smarty = new SmartyBC();
However, using {include_php file='./custom_php/manufacturers.php'} in theme's header.tpl still results in blank screen. Errors from php_error_log :
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Notice: Undefined property: SmartyBC::$trusted_dir in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\Smarty.class.php on line 676
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\themes\trzmiel\header.tpl" on line 101 "{include_php file='./custom_php/manufacturers.php'}" {include_php} file './custom_php/manufacturers.php' is not readable' in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php:667
Stack trace:
#0 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_compile_include_php.php(81): Smarty_Internal_TemplateCompilerBase->trigger_template_error('{include_php} f...', 101)
#1 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php(485): Smarty_Internal_Compile_Include_Php->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL)
#2 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templ in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 667
It seems that trusted_dir property is available only when using Security Policy:
class My_Security_Policy extends Smarty_Security {
public $trusted_dir = './custom_php/';
}
$smarty = new SmartyBC();
// enable security
$smartyBC->enableSecurity('My_Security_Policy');
so you should provide here correct path (I don't know is it relative or not - you should give a try) and then you should be able to use manufacturers.php file in your template file but you should also here look at correct file path.

Categories