I am trying to use Michelf's PHP implementation of Markdown.
I'm including his Markdown.php script with include() at the beginning of my main script, but when trying to use the class (be it for creating a new object or using a method directly), I get this:
Fatal error: Class 'Markdown' not found in [my main script]
The class Markdown is cleary defined in included Markdown.php however.
I've checked, of course, that the include works. I've tried placing Markdown.php in both my include_path and my main script's path, it doesn't change anything.
I am at a loss.
Judging by the source file, you most likely forgot to import the class before using it:
use Michelf\Markdown;
// ...
$md = new Markdown();
Alternatively, you could use the canonical name:
$md = new \Michelf\Markdown();
Related
I'm trying to include two php class file in my index file. But console returns the following error:
Cannot declare class Experience, because the name is already in use
in C:\Users\p-pri\wa\php_oop\es_freetime\experience.php on line 3
This is how I've included files in index.php:
<?php
include("./sport.php");
include("./relax.php");
Line 3 of experience.php
class Experience {
#restofmycode
What's wrong?
The problem is that you are importing that class file in multiple places. This causes it to be loaded twice.
To avoid this kind of problem, you can use require_once instead of your include calls. The syntax is the same:
require_once "./experience.php";
This will make php load this file just on first call. The. Other calls will be ignored, but the class will be available.
As suggested in comments, you can also use a autoloader to make things easier and simpler. See this article: https://medium.com/tech-tajawal/php-composer-the-autoloader-d676a2f103aa
I made a helper class which have lots of small functions that will help me to create my content, but when I try to include it in my code the PHP shows an error saying that my class doesn't exist.
I just use require_once('../general.php'); but it gives me a "failed to open stream" error.
Just add the class to application/classes and use as normal. I've used some kind of Util.php class with some static functions like that.
Oh and don't bother with loading it manually, autoloader should deal with it just fine.
Edit:
Make sure that your class starts with a capital letter (General.php) and call it just General in your code.
You just need to call it just by name like if you have Function.php
You can call it through (Function) no need to add php extension.
you should use General not General.php
I have a script with cURL to initiate other script that import products and categories in prestashop.
This is the script which start with cURL:
define('_PS_ADMIN_DIR_', getcwd());
include_once(_PS_ADMIN_DIR_.'/../config/config.inc.php');
include_once(_PS_ADMIN_DIR_.'/../config/defines.inc.php');
include_once(_PS_ADMIN_DIR_.'/functions.php');
include_once dirname(__FILE__).'/../controllers/admin/AdminImportController.php';
if (!isset($_GET['entity'])) die();
$import = New AdminImportController();
switch ($_GET['entity']) {
case 0:
loadCategoriesPost();
$import->categoryImport();
break;
case 1:
loadProductsPost();
$import->productImport();
break;
}
My problem is that the seconds script generate an error from "include_once dirname(FILE).'/../controllers/admin/AdminImportController.php';":
PHP Fatal error: Cannot redeclare class AdminImportControllerCore in...
I have tried to use include_once, DIR, also I looked for in that included files a line with "new AdminImportController();" but I don't found nothing.
Thanks!
Look inside the AdminImportController.php file. My guess is that it is also including the file that defines the AdminImportControllerCore class, and that it is not using the include_once verity of the include functions.
That would mean that your main page is defining the class through the include_once call, and then when it includes the AdminImportController class, the include there kicks in and tries to re-define the core class, with the results you described.
I suggest you look into PHP's autoloader feature. It'll save you all the headaches associated with manually including files like that. It also tends to promote good file and class naming practices.
I have the code below. When I run it I get error:
Fatal error: Cannot redeclare class Google_Account in
/var/www/vhosts/example.com/httpdocs/google-api-php-client
/src/contrib/Google_AnalyticsService.php on line 379
That's because both of the "Google_AdsenseService.php" and "Google_AnalyticsService.php" files have a class named Google_Account. The member variables and functions of Google_Account class are different in that files.
I need to get Adsense and Analytics data in the same time. So I need to use both of the services at once. I couldn't find a way to un-declare classes. How can I use both of the services together?
include_once APP.'Vendor/google-api-php-client/src/Google_Client.php';
$client1 = new Google_Client();
$client1->setApplicationName('aaa');
$client1->setDeveloperKey('1234');
$client1->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AdsenseService.php';
$client1->setClientId('2345');
$client1->setClientSecret('4444');
$service1 = new Google_AdsenseService($client1);
// some code that gets data from "$service1"
$client2 = new Google_Client();
$client2->setApplicationName('aaa');
$client2->setDeveloperKey('1234');
$client2->setRedirectUri('http://example.com/');
include_once APP.'Vendor/google-api-php-client/src/contrib/Google_AnalyticsService.php';
$client2->setClientId('4567');
$client2->setClientSecret('5555');
$service2 = new Google_AnalyticsService($client2);
// some code that gets data from "$service2"
You can add different namespaces at the top of each files in contrib directory. For example for Google_AdsenseService.php file add namespace Google\AdsenseService; at the top.
// Google_AdsenseService.php file
namespace Google\AdsenseService;
As long as the file contents are only referencing the contents from same file it'll will work. Only when you access it you access by namespace. Like this,
$service1 = new Google\AdsenseService\Google_AdsenseService($client1);
You have two options:
For PHP 5.3+, you can add a namespace at the start of the file. After this, you need to fix references to other classes from the modified class (Exception will become ::Exception, etc.)
You may rename the class in a text editor, this will be probably easier. Just open up the file in your favorite text editor, and use replace all. Change Google_Client to something else. There is a good change the lib won't use dynamic class construction and other funny stuff, so your quickly refactored code will work.
I'm somewhat new to PHP. I wrote static class that I would like to use and call, however when I try to call it I get a fatal because the php class file is not in the same directory as the calling file. Do I have to include it in order to use it?
Yes. Use include to include other PHP files. Even if your class file in the same directory, you still need to include it (you can also include files from other directories, obviously).
Yes. Any code that is in another file must be included. You can avoid explicitly loading the class in that page by using require() if you use an autoloader.
Yes. The class must be loaded to use it. This is true for instantiting it or calling a static function. Use require, require_once, include, or include_once to load the class file.