I am trying to create a function that changes the locale of a site based on the domain extension but when I try and set the value of a Variable in AppModel as a function I get an error. I am not sure what I am doing wrong.
FYI: $_SERVER['HTTP_HOST'] = '.de';
class AppModel extends Model {
//var $locale = 'de_de'; // Example of what I need
var $locale = $this->getLocale();
function getLocale() {
$domain = explode('.', $_SERVER['HTTP_HOST']);
if ($domain[1] == 'de') {
return 'de_de';
} else {
return 'en_gb';
}
}
}
Error Returned:
Parse error: syntax error, unexpected T_VARIABLE in /var/www/devsite/v1/site/app/app_model.php on line 7 Call Stack: 0.0002 671648 1. {main}()
/var/www/devsite/v1/site/app/webroot/index.php:0 0.0255 5883776 2. Dispatcher->dispatch() /var/www/devsite/v1/site/app/webroot/index.php:83 0.0264 5949592 3.
Dispatcher->__getController() /var/www/devsite/v1/site/cake/dispatcher.php:116 0.0264 5949672 4. Dispatcher->__loadController()
/var/www/devsite/v1/site/cake/dispatcher.php:385 0.0265 5951760 5. App->import() /var/www/devsite/v1/site/cake/dispatcher.php:413 0.0265 5953552 6.
App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0265 5954000 7. App->import()
/var/www/devsite/v1/site/cake/libs/configure.php:1171 0.0265 5957624 8. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0268 5984264 9.
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019 0.0269 6047416 10. require('/var/www/devsite/v1/site/app/app_controller.php')
/var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0269 6047560 11. App->import() /var/www/devsite/v1/site/app/app_controller.php:8 0.0270 6051456 12.
App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0270 6052240 13. App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1036
0.0272 6164128 14. require('/var/www/devsite/v1/site/cake/libs/sanitize.php') /var/www/devsite/v1/site/cake/libs/configure.php:1060 0.0272 6164416 15.
App->import() /var/www/devsite/v1/site/cake/libs/sanitize.php:2 0.0273 6165128 16. App->__settings() /var/www/devsite/v1/site/cake/libs/configure.php:916 0.0337 8579264 17.
App->import() /var/www/devsite/v1/site/cake/libs/configure.php:1149 0.0337 8582864 18. App->__find() /var/www/devsite/v1/site/cake/libs/configure.php:955 0.0338 8583952 19.
App->__load() /var/www/devsite/v1/site/cake/libs/configure.php:1019
Thanks in advance.
the error message says it pretty clearly: invalid php
you still have to code valid PHP (even if it is cakePHP)! using a proper IDE will outline the error right away:
var $uses ('App');
should be
public $uses = array('App');
as documented!
if you are still on PHP4 you would need "var" instead of "public"
you should really start reading basic php books because you seem to lack the basic stuff.
you can also not use dynamic methods in the class declaration:
var $locale = $this->getLocale();
you need to use the constructor for this:
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$this->locale = $this->getLocale();
}
Related
Confession:
I read many similar questions on this platform, but nothing seems closely aligned to my situation. Most of the questions seem to originate from binding params in prepared statements or execute statement.
In my case, the website runs smooth on a local server(Apache2). However, it throws the error below when published on live server.
Fatal error: Uncaught Error: Call to a member function signin() on boolean in /storage/ssd5/815/17670815/app/controllers/signin.php:16 Stack trace: #0 /storage/ssd5/815/17670815/app/core/app.php(33): Signin->index() #1 /storage/ssd5/815/17670815/public_html/index.php(4): App->__construct() #2 {main} thrown in /storage/ssd5/815/17670815/app/controllers/signin.php on line 16
Context
I'm using MVC (OOP) in PHP and here the relevant parts mentioned in the error. I hope this is not too much.
In the main index page, the line referred in the error is a core class(App) instantiation
<?php
session_start();
require_once '../app/initializer.php';
$app = new App(); //this is the line 4 mentioned in the error
In Signin controller class the line referred in the error is indicated below
<?php
class Signin extends Controller{
function index(){
//you can do this if passing data to view
$data["Page_title"] = "Signin";
if($_SERVER['REQUEST_METHOD'] == "POST"){
// this is a debuggin code
//echo "I am signin controller <br />";
// show($_POST);
$user = $this->loadModel("User");
$user->signin($_POST); //this the line referred in the error
}
$this->view("zac/signin",$data);
}
}
In class APP the line is a callback - check below
<?php
class App {
private $controller = "home";
private $method = "index";
private $params = [];
public function __construct()
{
$url = $this->splitURL();
if(file_exists("../app/controllers/".strtolower($url[0]).".php")){
$this->controller = strtolower($url[0]);
//unset the array position
unset($url[0]);
}
require "../app/controllers/".$this->controller.".php";
// echo file_get_contents('http://smart-ecom.000webhostapp.com/app/controllers/'.$this->controller.".php");
//Create instance of whatever controller class is passed(if it exists, otherwise the home controller)
$this->controller = new $this->controller;
if(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method =$url[1];
unset($url[1]);
}
}
$this->params = array_values($url);
call_user_func_array([$this->controller, $this->method],$this->params); //this is line 33
}
/**
* splitURL gets url from browser and processes against the conroller classes and their methods
* #return array
*/
private function splitURL(){
//check if the the GET is set otherwise set the url to defualt class home
$url = isset($_GET['url']) ? $_GET['url'] :"home";
// return explode("/",filter_var(trim($_GET['url'],"/"), FILTER_SANITIZE_URL));
return explode("/",filter_var(trim($url,"/"), FILTER_SANITIZE_URL));
}
}
?>
The Database class's read function is as follows. This method isn't directly referred in the error message
public function read($query, $data = []){
$stmt = self::$conn->prepare($query);
$result = $stmt->execute($data);
if($result){
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(is_array($data) && count($data) > 0){
return $data;
}
}
return false;
}
As I mentioned earlier, this error fires on a live server but the website runs smooth in dev environment with PHP 7.4, Apache2, MySQL 8 Windows 10.
Your help is match appreciated in advance.
I learned this the hard way and I hope this can help someone with similar issues. The cause of the error because of how windows and Linux deals with case sensitivity in file names. In Windows, file names aren't case sensitive while in Linux - they are. So, that was the reason why the website was running smooth in the local dev environment(Windows machine) but throwing an error on a live server(which is Linux). To see the difference, refer to my earlier comment in this thread.
protected function loadModel($model){
if(file_exists("../app/models/". strtolower($model) . ".class.php")){
include "../app/models/".strtolower($model).".class.php";
return $model = new $model();
}else{
return false;
}
}
}
In the "include" line, you can see that I added the strtolower function to include the proper model and that solved the issue.
I'm using dingo/api along with nWidart/laravel-modules packages to create an API.
We know that in nwidart package each Module can it's model and service provider.
Now suppose we have a PriceList model like this :
class PriceList extends Model
{
protected $primaryKey = 'price_list_id';
protected $fillable = ['title', 'name', 'country', 'rate', 'creator'];
public function __construct(array $attributes = [])
{
$attributes['creator'] = app('Dingo\Api\Auth\Auth')->user()->user_id;
parent::__construct($attributes);
}
}
Then in the PriceListServiceProvider of that module, for some reason , I need to fetch a specific price list and store id of that in config. For that I wrote these:
class PriceListServiceProvider extends ServiceProvider
{
protected $defer = FALSE;
public function boot()
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$priceList =
PriceList::where('rate', 'toman')->orderBy('created_at', 'asc')->first();
if ($priceList) {
$defaultPriceList = $priceList->price_list_id;
}
config('pricelist.default',$defaultPriceList);
}
}
But after running project I got this error every time :
Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 2 passed to Dingo\Api\Auth\Provider\JWT::authenticate() must be an instance of Dingo\Api\Routing\Route, null given, called in D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php on line 82 in file D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Provider\JWT.php on line 41
Stack trace:
1. Symfony\Component\Debug\Exception\FatalThrowableError->() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Provider\JWT.php:41
2. Dingo\Api\Auth\Provider\JWT->authenticate() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:82
3. Dingo\Api\Auth\Auth->authenticate() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:151
4. Dingo\Api\Auth\Auth->getUser() D:\wamp\www\zarsam-app\vendor\dingo\api\src\Auth\Auth.php:166
5. Dingo\Api\Auth\Auth->user() D:\wamp\www\zarsam-app\Modules\PriceList\Entities\PriceList.php:18
6. Modules\PriceList\Entities\PriceList->__construct() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1450
7. Illuminate\Database\Eloquent\Model->__callStatic() D:\wamp\www\zarsam-app\app\Providers\AppServiceProvider.php:37
8. App\Providers\AppServiceProvider->boot() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29
9. call_user_func_array() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:29
10. Illuminate\Container\BoundMethod->Illuminate\Container\{closure}() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:87
11. Illuminate\Container\BoundMethod->callBoundMethod() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:31
12. Illuminate\Container\BoundMethod->call() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Container\Container.php:549
13. Illuminate\Container\Container->call() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:792
14. Illuminate\Foundation\Application->bootProvider() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:775
15. Illuminate\Foundation\Application->Illuminate\Foundation\{closure}() [internal]:0
16. array_walk() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:776
17. Illuminate\Foundation\Application->boot() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php:17
18. Illuminate\Foundation\Bootstrap\BootProviders->bootstrap() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:213
19. Illuminate\Foundation\Application->bootstrapWith() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:162
20. Illuminate\Foundation\Http\Kernel->bootstrap() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:146
21. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() D:\wamp\www\zarsam-app\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116
22. Illuminate\Foundation\Http\Kernel->handle() D:\wamp\www\zarsam-app\public\index.php:55
And if I remove those lines, no errors occurred.
Even I moved codes to main AppServiceProvider but same error again occurred.
<?php
class TEST{
public $x=1;
private $y=2;
public function changeA($val){
//$this->x = $val;
echo "X-->".$this->x;
}
private function changeB($val){
//$this->y = $val;
echo "Y-->".$this->y;
}
}
$a = new TEST();
$a->changeA(3);
#
$a->changeB(4);
This is really bugging me, I use all the correct syntax but I got error right on the line I do CLASS test.
Parse error: parse error in file.php on line x
Tested:
-Remove vars, functions, new objects. nothing fix it.
====Updated Code above, but still, the same error.
I think there is something wrong with my php... I am now running all different kind of code, even echo return the same error. I think there is some other trouble with my php setup.
===Last update
I was using Ajax to passing value and write into a php file with 755 and public access. It was seem like a kind of process hiccup. Now it functioning correctly. But the example still, really useful. Well, don't know what is the vote down for, its seem make sense to mark reason for vote down as well like the ones who need to vote to close it. So SO can as least know the reason for the vote down. Interesting right? Someone who actually care about improving this.
Class method definitions are not statements, and therefore should not be terminated with ;.
This means that the }; on lines 11, 16 and 17 should simply be } instead.
On another note, I don't know what version of PHP you're using. I'm using PHP 5.5 and got a very clear message:
Parse error: syntax error, unexpected ';', expecting function (T_FUNCTION) in test.php on line 11
It's always good to practice on simple examples to make its own idea about how it works.
This might help to clarify things.
class test
{
public $x;
private $y;
function __construct() {
echo "-- Constructor --<br/>";
$this->changeX(1);
$this->changeY(2);
echo "-- Exiting Constructor --<br/>";
}
public function changeX($val) {
$this->x = $val;
echo "X-->".$this->x."<br/>"; // for debugging purpose only
}
private function changeY($val) {
$this->y = $val;
echo "Y-->".$this->y."<br/>"; // for debugging purpose only
}
public function changeYprivate($val) {
$this->changeY($val); // can call private method here
}
public function getY() {
return $this->y;
}
}
$objTest = new test();
echo "X is ".$objTest->x." and Y is ".$objTest->getY()."<br/>";
$objTest->changeX(3);
$objTest->x = 10; // ok x is public, it can be modified
$objTest->changeYprivate(4);
// $a->changeY(4); // Error : cannot call this function outside the class !
// $objTest->y = 20; // Error : y is private !
// echo $objTest->y; // Error ! Can't even read y because it's private
echo "X is ".$objTest->x." and Y is ".$objTest->getY()."<br/>";
Output:
-- Constructor --
X-->1
Y-->2
-- Exiting Constructor --
X is 1 and Y is 2
X-->3
Y-->4
X is 10 and Y is 4
I'm getting the following error in magento administration
Fatal error: Class 'Zend_Log' not found in /home/website/public_html/app/code/community/Uni/Fileuploader/Block/Adminhtml/Fileuploader/Edit/Tab/Products.php on line 241
This is a community extension, which has been working fine on my website. The error makes no sense to me, because the line 241 contains just a closing "}" character.
class Uni_Fileuploader_Block_Adminhtml_Fileuploader_Edit_Tab_Products extends Mage_Adminhtml_Block_Widget_Grid {
...
...
...
public function getRowUrl() {
return '#';
}
public function getGridUrl() {
return $this->getUrl('*/*/productgrid', array('_current' => true));
}
protected function getFileuploaderData() {
return Mage::registry('fileuploader_data');
}
protected function _getSelectedProducts() {
$products = $this->getRequest()->getPost('selected_products');
if (is_null($products)) {
$products = explode(',', $this->getFileuploaderData()->getProductIds());
return (sizeof($products) > 0 ? $products : 0);
}
return $products;
}
} // line 241, where error occurs
I can post the rest of the code, if you need it.
I noticed that if I upgrade to PHP 5.4 version the error disappears, but since 5.4 version causes other errors on my website, I have to continue using 5.3.
Any ideas on how to solve this?
The problem could be the name of one of the methods in your custom class.
Take for example the method name is getData() ,
Try searching for generic method names in your script, such as getData, which might be reserved by some of Magento’s core classes. I figure that these methods have predefined functionality, which your module is missing support for, and Zend then tries to write an exception to Zend log.
Reference link: netismine
I got the same error when rewriting a payment method.
public function authorize($payment, $amount)
Solved rewriting exactly the same main method:
public function authorize(Varien_Object $payment, $amount)
Magento 1.9.1.0/PHP 5.5
I am working on a website using someone else's source code called ecshop, a e-commerce website. I want to use PHPUnit to unit test my code but meet a problem.
This is what the error looks like:
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin>phpunit
--stderr wang_test.php PHPUnit 3.7.27 by Sebastian Bergmann.
E
Time: 1.03 seconds, Memory: 6.75Mb
There was 1 error:
1) ShopTest::test_get_shop_name Undefined index: ecs
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\includes\lib_common.ph
p:564
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\includes\init.ph
p:147
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\wang.php:10
C:\Users\maoqiuzi\Documents\Shanglian\XinTianDi\xintiandi\admin\wang_test.php:10
FAILURES! Tests: 1, Assertions: 0, Errors: 1.
The source code of wang_test.php:
<?php
require_once("wang.php");
class ShopTest extends PHPUnit_Framework_TestCase
{
public function test_get_shop_name()
{
$shop = new Wang();
$first_row_of_shop_list = $shop->get_shop_list();
}
}
The source code of wang.php:
<?php
class Wang
{
private $exchange;
function get_shop_list()
{
define("IN_ECS", 1);
require(dirname(__FILE__).'/includes/init.php');
$this->exchange = new exchange($GLOBALS['ecs']->table('shop'), $GLOBALS['db'], 'shop_id', 'shop_name');
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('shop');
$shop_list = $GLOBALS['db']->getAll($sql);
if($shop_list != array())
return $shop_list;
else
return array();
}
}
code in init.php
require(ROOT_PATH . 'includes/lib_common.php');
class ECS //line 82
{
var $db_name = '';
var $prefix = 'ecs_';
function ECS($db_name, $prefix)
{
$this->db_name = $db_name;
$this->prefix = $prefix;
}
...
}
...
$ecs = new ECS($db_name, $prefix); // line 114
... // other initialization codes here
$_CFG = load_config(); //line 147
code in lib_common.php
function load_config()
{
$arr = array();
$data = read_static_cache('shop_config');
if ($data === false)
{
$sql = 'SELECT code, value FROM ' . $GLOBALS['ecs']->table('shop_config') . ' WHERE parent_id > 0';
$res = $GLOBALS['db']->getAll($sql);
...
}
I've been working on this for days, and felt very frustrated. Hope anyone help me out! Thanks!!!
As you can see in PHPunit's manual part related on "How to test for PHP errors", how error_reporting is configured affects the test suite; which is your case.
You have (at least) three different options:
Fix the code to check and not use undefined indexes of arrays
Change the error_reporting to ignore notices (one of the examples in the link)
Create (and use) a phpunit.xml configuration file and set convertNoticesToExceptions to false
In init.php, if you change:
$ecs = new ECS($db_name, $prefix);
to:
$GLOBALS['ecs'] = new ECS($db_name, $prefix);
does it start to work (or at least move on to a different error message)?
What I'm thinking is that init.php is expecting it is running as global code, so isn't being explicit like that, but then PHPUnit is doing something clever such that it is not run as global code. So $ecs just ends up being treated as a local variable, not as a global.
(If it does change the error message, go through all other global code in your libraries and change them to use $GLOBALS[...] explicitly too. This is a GoodThing™ to do anyway, as it makes code clearer when other people look at it, and avoids easy-to-make mistakes when refactoring global code into functions.)