I read this article after upgrading my local dev environment to php8.
And implement it in my class, like:
<<Attribute('foo')>>
public function index()
{
$posts = (new Post)->get()->withMany('image');
return $this->app()->view('index', compact('posts'));
}
and it return error.
syntax error, unexpected token "<<", expecting "function" or "const"
What is really a proper of doing that in php8. Am i missing something?
Correct attribute syntax in PHP 8 is:
#[Attribute('foo')]
https://www.php.net/releases/8.0/en.php
public function create(Request $request)//create object of the request
{
$student = new Student;//create object of the model
$student->name = $request->name;
$student->city = $request->city;
$student->marks = $request->marks;
$student->save();
return redirect('index');//if we write this out of the curly braces then this error show
}
Related
I am practicing with facebook PHP api
when i am trying to make a login function
here comes out this warnning
syntax error, unexpected T_OBJECT_OPERATOR in /home/u528851895/public_html/Desktap/facebook-php-sdk-v4-4.0-dev/src/Facebook/FacebookResponse.php on line 137
but it's ok when i use mamp localhost
here's line 136~138 in FacebookResponse.php:
public function getGraphObject($type = 'Facebook\GraphObject') {
return (new GraphObject($this->responseData))->cast($type);
}
Upgrade your php version or do something like this
public function getGraphObject($type = 'Facebook\GraphObject') {
$obj = new GraphObject($this->responseData);
return $obj->cast($type);
}
i am trying to upload my codeigniter website on 000webhost server . but its giving me a syntax error
syntax error, unexpected '[', expecting ')' in
/home/a4703701/public_html/application/controllers/update.php
but its working correct on localhost.
the error is in model my model is
public function user($id=NULL)
{
if($id=='')
{
$q = $this->db->get('user');
if($q->num_rows()>0)
{
return $q;
}
}
else
{
$q = $this->db->get_where('user',['id'=>$id]);
if($q->num_rows()>0)
{
return $q->row();
}
}
}
any idea how to remove this error ?
You're almost certainly not running PHP 5.4 or newer but are trying to use syntax only available in those versions.
['id'=>$id]
is shorthand array syntax and was introduced in PHP 5.4. You need to replace it with:
array('id'=>$id)
to be backwards compatible with prior PHP versions.
The code is
// Get singleton (first value from row with single value)
static function singleton($arg, $params = false) {
return pg_fetch_row(SQL($arg, $params))[0];
}
The error message is
2014-02-19 12:54:23: (mod_fastcgi.c.2701) FastCGI-stderr: PHP message: PHP Parse error: syntax error, unexpected '[' in /var/www/blockexplorer.com/htdocs/includes/sql.inc on line 69
I think there is a config that can fix it.
This depends on PHP version you are using. If you are using PHP 5.4 or above then your code will not give error otherwise you will have to store the result in a variable and use it.
Reference : PHP 5.4
Look for "Array Dereferencing" here.
Put the result of the function in a variable
static function singleton($arg, $params = false) {
$foo = pg_fetch_row(SQL($arg, $params));
return $foo[0];
}
PHP does not support anonymous arrays. Use an named array instead:
static function singleton($arg, $params = false) {
$row=pg_fetch_row(SQL($arg, $params));
return $row[0];
}
Slightly confused with PHP because it does not declare object variable types.
This is simplified code which does not work. I get why I get an error but not sure how in PHP I can specify that $pb is a PushBot object and so it has methods which can be used.
class Bot
{
public $pb;
//Constructor
function __construct(){
require_once('../PushBots.class.php');
// Application ID
$appID = '';
// Application Secret
$appSecret = '';
// Push The notification with parameters
$this ->pb = new PushBots();
$this ->pb->App($appID, $appSecret);
}
//Method. The $this->pb->Push() does not work
public function sendMessage(){
$this->pb->Push();
}
}
//Client calling the class
$bot = new Bot();
$bot->sendMessage();
The error I get is :
Parse error: syntax error, unexpected '$' for when the line
$this->pb->Push();
is called.
I guess its because it does not know that $pb is a PushBot object at this stage ?
Can I not declare it something like :
Public Pushbot $pb;
Parse error: syntax error, unexpected '$' for when the line
This is a parse error, meaning your code has not even been run yet. Check your code for any sytnax errors (the code you posted does not contain the syntax error).
how in PHP I can specify that $pb is a PushBot object
Although unrelated to the syntax error, if you were using some sort of dependency inversion you could use type-hinting to require a certain object to be passed:
// will cause fatal error if anything other than an object of type Pushbot is passed
function __construct(Pushbot $pb)
According to Symfony2 documentation, I can access to instances in database with the next code:
public function showAction($id)
{
$producto = $this->getDoctrine()
->getRepository(’AcmeGuardaBundle:Producto’)
->find($id);
$nombreCategoria = $producto->getCategoria()->getNombre();
// ...
}
However, I try to do:
$imagen = $this->getDoctrine()
->getRepository(’MSDHomeBundle:Imagen’)
->find($_SESSION['id']);
and I get the next error in the browser:
Parse error: syntax error, unexpected ':' in /home/manolo/MiServer/itransformer-2.0/src/MSD/HomeBundle/Controller/HomeController.php on line 219
What am I doing wrong? Imagen is a class stored in database, and persisting data works right.