Including a file with PHP - avoiding not found? - php

I am new to php, I have two files, one where I want to keep my classes and bulk of my code, and another where I integrate with some HTML. My code was working, but now I am getting the error:
PHP Fatal error: Uncaught Error: Class 'bardrink' not found in C:\MAMP\htdocs\index.view.php:84 Stack trace: > #0 {main} thrown in C:\MAMP\htdocs\index.view.php on line 84
Here is my index.php file:
<?php
class bardrink {
public $drink_name;
public $drink_desc;
public $drink_strength;
public $drink_price;
public function __construct($drink_name,$drink_desc,$drink_strength,$drink_price)
{
$this->drink_name = $drink_name;
$this->drink_desc = $drink_desc;
$this->drink_strength = $drink_strength;
$this->drink_price = $drink_price;
}
}
require 'index.view.php';
?>
And in my index.view.php I am adding:
<?php
$barselection = [
new bardrink('Water','A light refreshing drink.',0,1),
new bardrink('Light Ale','A pale ale brewed locally.',2,4),
new bardrink('Mulled Wine','A warm festive wine, perfect for a chilly evening!',3,6),
new bardrink('Dark Ale','A strange frothy, dark liquid...',5,10)
];
var_dump($barselection)
?>
I've required the index.view.php in the index file, but I do I need to add a require into my view file also to make this work?

Related

Converting files using PHP LibreOffice and the ncjoes/office-converter library

I am using ampps as a windows 10 apache server, my php version is 7.3. I downloaded it from the LibreOffice download page and installed it on my computer. Then I installed this library via composer https://github.com/ncjoes/office-converter. I try as in the example given, but it does not convert and gives an error. I would be very grateful if you could help me where I am wrong. Here is my code sample and the error I encountered:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
use PHPUnit\Framework\TestCase;
class OfficeConverterTest extends TestCase
{
/**
* #var OfficeConverter $converter
*/
private $converter;
private $outDir;
public function setUp()
{
parent::setUp();
$DS = DIRECTORY_SEPARATOR;
$file = __DIR__."{$DS}sources{$DS}test.docx";
$this->outDir = __DIR__."{$DS}results";
$this->converter = new OfficeConverter($file, $this->outDir);
}
public function testDocxToPdfConversion()
{
$output = $this->converter->convertTo('result.pdf');
$this->assertFileExists($output);
}
public function testDocxToHtmlConversion()
{
$output = $this->converter->convertTo('result.html');
$this->assertFileExists($output);
}
}
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
?>
Fatal error: Uncaught Error: Call to a member function convertTo() on null in C:\Program Files\Ampps\www\converter\converter.php:29 Stack trace: #0 C:\Program Files\Ampps\www\converter\converter.php(43): OfficeConverterTest->testDocxToPdfConversion() #1 {main} thrown in C:\Program Files\Ampps\www\converter\converter.php on line 29
When running tests, we are supposed to leave running them to phpunit, but you are trying to call manually (in last 2 lines of your code).
But if you insist on calling manually, change:
$donustur = new OfficeConverterTest();
$donustur->testDocxToPdfConversion();
Into:
$donustur = new OfficeConverterTest();
$donustur->setUp();
$donustur->testDocxToPdfConversion();
So that you call setUp() which phpunit would normally call for you automatically.
See also:
How do I run all my PHPUnit tests?
How to run single test method with phpunit?
Example for page
If you want to use this logic on a Web-Page, it should look something like:
<?php
if (!file_exists(__DIR__.'/vendor/autoload.php')) echo 'autoload.php mevcut değil!';
else require __DIR__.'/vendor/autoload.php';
use NcJoes\OfficeConverter\OfficeConverter;
echo 'Converting...<br>';
$input = __DIR__ . '/test.docx';
$converter = new OfficeConverter($input, __DIR__.'/results');
$output = $converter->convertTo('result.pdf');
echo 'Saved at:' . $output . '<br>';

how to fix this problem(Array to string conversion)

this is my url http://localhost/mymvc/index.php?url=controller/method
I want to catch 'method' by $_GET Super Global Variable and get access class of Controller method.
I want to get all method dynamic way
Here is my index file code.
<?php
include 'inc/header.php';
include_once 'system/libs/MController.php';
include_once 'system/libs/Main.php';
$url = $_GET['url'];
$url = rtrim($url, '/');
$url = explode("/", $url);
include 'app/controllers/'.$url[0].'.php';
$ctlr = new $url[0]();
$ctlr->$url[1](); //---->(1) here is problem. I can't access my method this way
include 'inc/footer.php';
?>
Here is my Controller.php file code
<?php
/**
* controller
*/
class Controller extends MController{
public function __construct(){
// parent::__construct();
}
public function method(){
echo "Mohammad Arman from method";
}
}
?>
But when I show this output. Showing below this error and notice
**
1)Notice: Array to string conversion in
C:\Users\USER\Documents\wamp\www\mymvc\index.php on line 15
2)Notice: Undefined property: Controller::$Array in
C:\Users\USER\Documents\wamp\www\mymvc\index.php on line 15
3)Fatal error: Uncaught Error: Function name must be a string in
C:\Users\USER\Documents\wamp\www\mymvc\index.php:15 Stack trace: #0
{main} thrown in C:\Users\USER\Documents\wamp\www\mymvc\index.php on
line 15**
You need to make sure the name you are using as the function name is properly delimited,
$ctlr->$url[1]();
could be interpreted as $ctlr->$url and then [1], but what you really want is...
$ctlr->{$url[1]}();

While trying to passing dotnet object one from another php file , Fatal error: Call to undefined method dotnet::

I have a project on PHP and I have to use a DOTNET DLL which was written on C# . I have to call different functions of the same object in two different PHP pages. In the first page it works but I get this error in the second page. Can you please help me? These are example ;
DOTNET DLL :
namespace FirstDotNet
{
[ComVisible(true)]
public class Class1
{
public string SampleFunction()
{
return "hello";
}
}
}
PHP Class File animals.php
class Animal{
var $abc;
public function do_it(){
$this->abc = new DOTNET("FirstDotNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxx", "FirstDotNet.Class1");
}
}
First PHP FILE a.php (this works, output is 'Hello')
require_once("animals.php");
session_start();
$first_animal = new Animal();
$_SESSION["animal"] = $first_animal;
$first_animal->do_it();
echo $first_animal->abc->SampleFunction();
Second PHP FILE b.php (this doesnt work, output is Fatal error: Call to undefined method dotnet::SampleFunction())
require_once("animals.php");
session_start();
$animal2 = $_SESSION["animal"];
echo $animal2->abc->SampleFunction();

Laravel: call class methods from php script in public directory

I have a view.blade.php that calls a wrapper.php that return some value for an iframe in the view.
I'm able to return that value, but I have to do some work with other methods in my laravel classes.
It's possible to instatiate the classes inside the wrapper php and call methods, or call static class methods?
Project structure:
- project
-- app
--- Http
---- Classes
----- RepositoryUtil.php
...
-- public
--- Wrapper.php
...
-- resources
--- views
---- partials
----- view.blade.php
Code inside view.blade.php:
<iframe id="reader" src="/libs/pdfjs/web/viewer.html?file=http://project.dev/Wrapper.php?id={{$encrypted}}">
</iframe>
Code inside Wrapper.php:
<?php
// tried: use App\Http\Classes\RepositoryUtil
// tried: \App\Http\Classes\RepositoryUtil::getValue();
// tried: {{RepositoryUtil::getValue()}}
$myValue = RepositoryUtil::getValue(); #not work
var_dump($myValue);
?>
Code inside RepositoryUtil.php
<?php
class RepositoryUtil{
public static function getValue(){
dd("getValue!");
return "value";
}
}
?>
The error:
( ! ) Fatal error: Class 'RepositoryUtil' not found in /home/vagrant/Code/project/public/Wrapper.php on line 14
EDIT:
I can call static class method adding include_once("../App/Http/Classes/RepositoryUtil.php"); at the top of Wrapper.php but when I call the "laravel methods" like $decrypted = Crypt::decrypt($encrypted); it return the error:
( ! ) Fatal error: Class 'Crypt' not found in /home/vagrant/Code/project/App/Http/Classes/RepositoryUtil.php on line 13
Thanks
After many and many attemps and research, I figure that out!
Post my Wrapper.php code:
<?php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$value = RepositoryUtil::getValue();
// Your other staff here...
?>
The problem was that after calling external php, Laravel wasn't booted!
I tried to boot with require __DIR__.'/../bootstrap/autoload.php';, $app = require_once __DIR__.'/../bootstrap/app.php'; and calling $app->boot(), and it worked.
But there were other problems: Facades were not loaded, but I can see into "alias" array when log $app variable.
You have to boot kernel to get Facades back.
Using Laravel 5.1.

declaring object in variable variable fatal error accessing empty property

Trying to crate objects dynamically for a plug in system (work in progress)
heres my code using $this->module->load_module('test'); to use the method that creates the dynamic objects. Following code is the function that loads the class's and makes use of an auto loader, i have checked that its getting the correct file etc.
<?php
class BaseModule {
function __construct() {
}
function load_module($module){
echo 'Module = '.$module.'<br />';
$object_name = $module . "Controller";
$this->$$module = new $object_name();
}
}
Here is a test module that it would load when invoking $this->module->load_module('test'); and it creates the object outputting the test strings via echo statements. Heres the code for the test module that was constructed. Which should cause no problems as there is not really a solution but just an output string, but posted any way.
<?php
class testController {
function __construct() {
echo 'test controller from modules <br />';
}
}
However when running the page i am getting some errors can any one help out?
Notice: Undefined variable: test in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
Fatal error: Cannot access empty property in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11

Categories