I am trying to call a third party library function in CakePHP from a controller file.
I have this in my controller file:
public function index() {
App::import('vendor', 'simple-html-dom.php');
App::import('vendor', 'utils.php');
set_time_limit(0);
$html = file_get_html("google.com");
...
}
I also have in app/vendor both simple-html-dom.php and utils.php files.
file_get_html is a public function in simple-html-dom.php (and it doesn't belong to any class). I end up with this error:
Error: Call to undefined function file_get_html()
I have been trying to search for how to solve this, but I have not found an answer.
I got mine working. Try this,
App::import('Vendor', 'simple_html_dom', array('file'=>'simple_html_dom.php'));
$html = file_get_html("google.com");
Try
public function index() {
App::import('vendor', 'simple-html-dom.php');
App::import('vendor', 'utils.php');
set_time_limit(0);
$SimpleHtmlDom = new SimpleHtmlDom(); // create object for html dom
$html = $SimpleHtmlDom->file_get_html("google.com");
}
Make sure simple-html-dom.php file contain class then you need to create object of that class after loading vendor.
because to access methods and property of class you need to create object of that class.
You can also access method with in same class using Self::file_get_html(); but this is for inside class declaration.
More help
App::import('Vendor', 'example', array('file' => 'Example.php'));
$example = new Example();
in above code i am including vendor file.
explanation
Above code will load Example.php file which is inside vendors/example directory.
In your case your vendor file is not properly loaded that's why you are getting class not found error.
Related
I'm trying to include a class in PHP and to use the methods from this class in another file. My code looks like that (small reproducable example). By the way: I'm working on a WordPress environment.
Main file:
include 'class-file.php';
$cars = new Cars();
var_dump($cars->getCars());
File class-file.php:
class Cars {
public function getCars() {
return "No Cars";
}
}
I get the following error:
Fatal error: Uncaught Error: Call to undefined method Cars::load() in /..../wp-content/themes/builder/includes/elements.php
Seems that my class instance is connected with another class from the theme. But why? Can I disconnect the class from any others? If yes, how?
Maybe the theme doesn't use namespaces, which makes all its classes discoverable from the global namespace, and by defining your own Cars class, you override the theme's class.
Try to define your namespace, and check if the conflict is gone.
Main file:
include 'class-file.php';
$cars = new \mycode\Cars();
var_dump($cars->getCars());
File class-file.php:
namespace mycode;
class Cars {
public function getCars() {
return "No Cars";
}
}
I am currently using twig standalone (not with symphony or composer) and am not finding in the documentation on how to register an extension in php.
My index.php file looks like this
<?php
include 'exts/ext.php';
require_once 'Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader);
$twig->addExtension(new Test_Twig_Extension())
echo $twig->render('index.twig');
?>
My extension class looks like this (ext.php)
<?php
//require_once 'Twig/Extension.php';
//require_once 'Twig/ExtensionInterface.php';
class Test_Twig_Extension extends Twig_Extension {
public function getFunctions() {
return array(
new Twig_SimpleFunction('my_function', array($this,'my_function'))
);
}
public function my_function($arg1, $arg2) {
echo "Arg1: {$arg1} and Arg2: {$arg2}";
}
public function getName(){
return 'my_function';
}
}
?>
I get the following error:
Fatal error:
Interface 'Twig_ExtensionInterface' not found in
C:\xampp\htdocs\Twig\Extension.php on line 12
I have found tones of articles with setting it up with yaml but I am not using yaml.
I am sure I am not registering this properly or do not have something set up just right.
So what I figured out is is the following:
When autoloading you do not need to require/include the class file
The name of your extension should start with Twig_Extension_ (for example i had to rename mine to Twig_Extension_Test rather than Test_Twig_Extension like the docs some time show)
Make sure your Twig_Extension_Test class has the getName method in it.
Name your extension file to be last part of class name. So mine had to be called Test.php. I believe this is case sensative as well.
Place this file into the Twig/Extension/ folder
Call $twig->addExtension(new Twig_Extension_Text());
I have updated my code in the Question to reflect these steps
I basically have the following directory structure
MiniCrawler
Scripts/
htmlCrawler.php
index.php
This is the index.php
use Scripts\htmlCrawler;
class Main
{
public function init()
{
$htmlCrawler = new htmlCrawler();
$htmlCrawler->sayHello();
}
}
$main = new Main();
$main->init();
And this is the /Scripts/htmlCrawler.php
namespace Scripts;
class htmlCrawler
{
public function sayHello()
{
return 'sfs';
}
}
The code throws the following error
Fatal error: Class 'Scripts\htmlCrawler' not found in
/mnt/htdocs/Spielwiese/MiniCrawler/index.php on line 9
You forgot to include the file /Scripts/htmlCrawler.php in your index.php file.
require_once "Scripts/htmlCrawler.php";
use Scripts\htmlCrawler;
class Main
{
public function init()
{
$htmlCrawler = new htmlCrawler();
$htmlCrawler->sayHello();
}
}
$main = new Main();
$main->init();
Your index file cannot find the definition of the htmlCrawler file if you never provide the file defining this class, and the use of namespaces doesn't automatically include the required classes.
The reason why frameworks don't require you to include manually the file and you can simply add the use statement is because they're handling the inclusion of required classes for the developer. Most of the frameworks are using composer to handle the automatic inclusion of the files.
You can obtain a somewhat similar functionality using autoloading.
I am using codeigniter 3
in application/config/config.php file I have added this autoload code for model
function __autoload($class) {
if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
include_once(APPPATH."models/".strtolower($class).EXT);
}
}
to autoload model
and I am using model in controller like this
public function index()
{
$post = new post();
}
but it is showing error
Class 'post' not found
I do have post model in model folder already created
I am using the autoload code from source
http://code.tutsplus.com/tutorials/6-codeigniter-hacks-for-the-masters--net-8308
but it is not working like shown in blog.
Do I need anything else to update more for this?
If you need to autoload a model in your CI3 app, just go in application/config/autoload.php and find the line :
$autoload['model'] = array();
Then, add the model you want to autoload :
$autoload['model'] = array('my_model', 'my_second_model');
Then in your controller, you don't need to create a new instance of your model class. Example :
$res = $this->my_model->myfunction();
Use capital letter for your class name.
Btw. I agree with first answer.
While trying to access a user class object from codeigniter helper file, its throwing error like Class 'User' not found. My code is something like
$u = new User();
$u->get();
I am able to use this in library files but not in helper files. Can somebody help me.
In order to use model in helper you have to:
// Get a reference to the controller object
$CI = get_instance();
// You may need to load the model if it hasn't been pre-loaded
$CI->load->model('User');
// Call a function of the model
$CI->User->get();
hope it will help!