Syntax error when accessing an array [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem. I've created a class 'Game', and in this class I have an array named 'ShadowField'. This Field contains objects ('Fields'):
class Game extends Gameboard
{
public $ShadowField = array();
public $GameField = array();
public $Home = array();
function __construct($c,$h,$w,$m,$fc){
parent::__construct($c,$h,$w);
$counter = 1;
for($index_h=0; $index_h<11; $index_h++){
for($index_w=0; $index_w<11; $index_w++){
array_push($this->ShadowField, new Field(parent::$this, $m, "FF8000", ($h/11.1)*$index_h, ($w/11.15)*$index_w, true, $counter, $index_h, $index_w));
$counter++;
}
}
$PlayerRed = new PlayerHome($this, $m, "FF0000", 0);
}
}
Then, in my next class, I tried to use this array, but then I got an error:
for($i=0;$i<4;$i++){
parent::__construct($game,$m,$c,$game->ShadowField[$FieldID[$i]]->Top,ShadowField[$FieldID[$i]]->Left);
}
Error:
Parse error: syntax error, unexpected '[' in C:\XAMPP\xampp\htdocs\ot\madn\Game.class.php on line 63
I don't understand it because it's an array, and I am calling it like an array, so what is the problem?
The "FieldID" is an array, too:
class PlayerHome extends Field
{
public $Color;
public $Player;
public $FieldID = array();
function __construct(Game $game, $m, $c, $p){
switch($p){
case 0: array_push($FieldID,1,2,12,13);
case 1: array_push($FieldID,10,11,21,22);
case 2: array_push($FieldID,100,101,111,112);
case 3: array_push($FieldID,109,110,120,121);
default:echo "Player_$p existiert nicht, bitte eine SpielerID zwischen 0-3 wählen.";
}
for($i=0;$i<4;$i++){
parent::__construct($game,$m,$c,$game->ShadowField[$FieldID[$i]]->Top,ShadowField[$FieldID[$i]]->Left);
}
}
}
(p.s.: Sorry for bad english)

You use it correctly the first time:
$game->ShadowField[$FieldID[$i]]->Top
However the second time you call ShadowField by itself without referencing the object:
ShadowField[$FieldID[$i]]->Left
Replace the line in the loop with this (formatted for visibility):
parent::__construct($game,
$m,
$c,
$game->ShadowField[$FieldID[$i]]->Top,
$game->ShadowField[$FieldID[$i]]->Left);

Related

syntax error, unexpected T_DOUBLE_ARROW inside class method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Have been stuck with this little issue, when using key value arrays inside a class |I will get an error syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'
class hello{
public function name(){
$allnames = array("name"=>"mark" , "role" => "admin");
}
}
I have checked your code in my system there is no any error in your code
<?php
class hello{
public function name(){
$allnames = array("name"=>"mark" , "role" => "admin");
return $allnames;
}
}
$obj = new hello;
print_r($obj->name())
?>
Please check
You may try assign array as
$allnames = ["name"=>"mark" , "role" => "admin"];

PHP class doesn't recognize this->method() [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When trying to access a method I've defined in a PHP class from within another method in the same class, I get the following error:
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /var/www/.../TOC.php on line 57
Previously, I'd these functions sitting outside of any class, just including them where I needed them. Having moved them into one, at first I thought I must be having the issue this user was having, but with an instance of the class, I can call my render method from another file with no problems. Commenting out the line this->printTreeArray($sectionProjects); eliminates the error. Here's my class:
<?php
class TOC{
private function printTreeArray($sectionProjects){
echo "var TOCnodes = [\n";
//Print each section, with another loop to print each one's problems
$i = 0;
foreach($sectionProjects as $sectionProject){
if($i != 0){
echo ",\n";
}
$project = $sectionProject->getProject();
//Get due date for mouseover text
$due = $project->getDueDate('F jS, Y h:i A');
$q = new ProjectProblemQuery();
$projectProblems = $q->findByRelProjectId($project->getId());
$pId = $i + 1;
echo "{id: $pId, pId: 0, name: \"Project $pId\", title: \"Due: $due\", isParent: true, open: true}";
//Print this section's problem list
$probId = 1;
foreach($projectProblems as $projectProblem){
echo ",\n";
$nodeId = ($pId * 10) + $j;
echo "{id: $nodeId, pId: $pId, name: \"$pId.$probId\", url: \"#\", target: \"_top\"}";
$j++;
}
$i++;
}
echo"];\n";
}
public function render($section){
if(! $section instanceof Section){
echo "$section is not an instance of Section!";
}
else{
echo "<ul id=\"TOCtree\" class=\"ztree\"></ul>\n";
echo "<script type=\"text/javascript\">\n";
$sectionProjects = $section->getSectionProjects();
this->printTreeArray($sectionProjects);
echo "$(document).ready(function () {
$.fn.zTree.init($(\"#TOCtree\"), setting, TOCnodes);
});";
echo "</script>";
}
}
}
This is my first time asking a question on SO - generally I've been able to find answers to my coding dilemmas, but this one has me baffled. I realize there's a lot of not-elegant stuff going on in this class, so if there's a point of style I can use to avoid this sort of error in the future, I'd greatly appreciate getting to learn it.
You should use $this instead of this.

I get this error Parse error: syntax error, unexpected '[' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
<?php
// configuration
require("../includes/config.php");
render("home_form.php", ["title" => "Log In"]);
?>
/**
* Renders template, passing in values.
*/
function render($template, $values = [])
{
// if template exists, render it
if (file_exists("../templates/$template"))
{
// extract variables into local scope
extract($values);
// render header
require("../templates/header.php");
// render template
require("../templates/$template");
// render footer
require("../templates/footer.php");
}
// else err
else
{
trigger_error("Invalid template: $template", E_USER_ERROR);
}
}
replace render("home_form.php", ["title" => "Log In"]);
with
render("home_form.php", array("title" => "Log In"));
and
$values = []
with
$values = array()
Which PHP version do you use?
Short array syntax is available since PHP 5.4.0: http://php.net/manual/en/migration54.new-features.php

unexpected T_IF, expecting ')' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Parse error: syntax error, unexpected T_IF, expecting ')'
What should I do?
$show_smileys = true;
$find = array(
'!\[h1\](.+)\[/h1\]!isU',
'!\[b\](.+)\[/b\]!isU',
'!\[i\](.+)\[/i\]!isU',
'!\[u\](.+)\[/u\]!isU',
'!\[strike\](.+)\[/strike\]!isU',
'!\[spoiler\](.+)\[/spoiler\]!isU',
'!\[url=(.*?)\](.*?)\[/url\]!is',
'!\[player\](.+)\[/player\]!isU',
'!\[quote=(.*)\]!siU',
'!\[/quote\]!si'
if ($show_smileys == true) {
,'#:p#is',
'#:eek:#is',
'#:rolleyes:#is',
'#;\)#is',
'#:o#is',
'#:D#is',
'#:\(#is',
'#:mad:#is',
'#:\)#is',
'#:cool:#is'
}
);
You can't include an if condition inside an array. Define the primary portion of your array like this:
$find = array(
'!\[h1\](.+)\[/h1\]!isU',
'!\[b\](.+)\[/b\]!isU',
'!\[i\](.+)\[/i\]!isU',
'!\[u\](.+)\[/u\]!isU',
'!\[strike\](.+)\[/strike\]!isU',
'!\[spoiler\](.+)\[/spoiler\]!isU',
'!\[url=(.*?)\](.*?)\[/url\]!is',
'!\[player\](.+)\[/player\]!isU',
'!\[quote=(.*)\]!siU',
'!\[/quote\]!si'
);
And then use a condition to merge the rest:
if ($show_smileys == true) {
$smiles = array(
'#:p#is',
'#:eek:#is',
'#:rolleyes:#is',
'#;\)#is',
'#:o#is',
'#:D#is',
'#:\(#is',
'#:mad:#is',
'#:\)#is',
'#:cool:#is'
);
$final = array_merge($find, $smiles);
}
You can't put if in the middle of an array, it can only be used where statements are allowed. Do it this way:
$find = array(
'!\[h1\](.+)\[/h1\]!isU',
'!\[b\](.+)\[/b\]!isU',
'!\[i\](.+)\[/i\]!isU',
'!\[u\](.+)\[/u\]!isU',
'!\[strike\](.+)\[/strike\]!isU',
'!\[spoiler\](.+)\[/spoiler\]!isU',
'!\[url=(.*?)\](.*?)\[/url\]!is',
'!\[player\](.+)\[/player\]!isU',
'!\[quote=(.*)\]!siU',
'!\[/quote\]!si'
);
$smileys = array(
'#:p#is',
'#:eek:#is',
'#:rolleyes:#is',
'#;\)#is',
'#:o#is',
'#:D#is',
'#:\(#is',
'#:mad:#is',
'#:\)#is',
'#:cool:#is'
);
if ($show_smileys) {
$find = array_merge($find, $smileys);
}

PHP: Catchable fatal error: Object of class stdClass could not be converted to string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I get the following dump & error when running the attached code. What I'm confused by is that $procID appears to be returned as a string, but as soon as I attempt to pass it again, its an object? How do I get it to be/stay a string? Thanks.
object(stdClass)#2 (1) {
["processId"]=> string(13)
"Genesis114001" } string(311)
"Genesis114001" string(293) " Genesis
" Catchable fatal error: Object of
class stdClass could not be converted
to string in
C:\wamp\www\SugarCE\testSOAPShawn.php
on line 15
<?php
set_time_limit(0);
require_once('nusoap.php');
require_once('BenefitSOAP.php'); //WSDL to PHP Classes
$client = new SoapClient('C:\wsdl\BenefitDeterminationProcess_BenefitDialogueServiceSOAP.wsdl', array('trace' => 1));
$procID = $client->start(array("prefix"=>"Genesis"));
$respXML = $client->__getLastResponse();
$requXML = $client->__getLastRequest();
echo "<p/>";
var_dump($procID);
//echo "<p/>";
var_dump($respXML);
//echo "<p/>";
var_dump($requXML);
$exchange = $client->exchangeOptions(array("processId"=>$procID)); //LINE 15
$end = $client->stop(array("processId"=>$procID));
?>
Whatever the $client->start() method is returning, it is typed as an object. You can access the properties of the object using the -> operator:
$procID = $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions(array("processId"=>$procID->processId));
This was probably an array, but is getting typed into an object. Thus, you end up with the stdClass.
Another (and possibly better) way to do this is to type the return. That way, you don't have to make a new array for later passing as argument:
$procID = (array) $client->start(array("prefix"=>"Genesis"));
...
$exchange = $client->exchangeOptions($procID);
$end = $client->stop($procID);

Categories