1

I am wondering my class property $friend_username does not returning its value either it is public.

update

class Feed { public static $friend_username; // ONLINE FRIENDS LOGIC public function online_friends(){ $friendsHTML = ''; $countOnlineFriends = ''; if(GetFriends($GLOBALS['log_username']) != false) { $all_friends = GetFriends($GLOBALS['log_username']); $orLogic = ''; foreach($all_friends as $key => $user){ if(IsBlocked($GLOBALS['log_username'],$user,true) == false){ $orLogic .= "username='$user' OR "; } } $orLogic = chop($orLogic, "OR "); $sql = "SELECT username, avatar, logged_in FROM users WHERE ($orLogic) AND logged_in = 1"; $query = mysqli_query($GLOBALS['db_conx'], $sql); $friend_loggedIn = array(); while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { $this->friend_username = $row["username"]; $friend_avatar = $row["avatar"]; $friend_loggedIn[] = $row["logged_in"]; $friend_pic = userImage($this->friend_username,$friend_avatar,'42','42',$link = false,$up = true); $friendsHTML .= '<li><a href="#" onClick="chatbox(\''.$this->friend_username.'\',\''.getName($this->friend_username,true).'\');return false;">'.$friend_pic.' '.getName($this->friend_username,true).'</li>'; $countFriends = count($friend_loggedIn); $countOnlineFriends = ($countFriends > 0) ? '<span class="online_friends animated">'.$countFriends.'</span>' : ''; } }else{ $friendsHTML = 'No friends'; } return "$countOnlineFriends|$friendsHTML"; } public function update_chat() { $id = ''; $messages = ''; $randUser = ''; $user = sanitize($this->friend_username); $sql = "SELECT * FROM pm_chat WHERE (sender='$GLOBALS[log_username]' AND receiver='$user') OR (sender='$user' AND receiver='$GLOBALS[log_username]') ORDER BY datetime DESC"; $result = mysqli_query($GLOBALS['db_conx'],$sql) or die(mysqli_error($GLOBALS['db_conx'])); while ($row = mysqli_fetch_assoc($result)) { $id = $row['id']; $user1 = $row['sender']; $user2 = $row['receiver']; $message = parseData($row['message']); $did_read = $row['did_read']; $datetime = $row['datetime']; if ($user1 != $GLOBALS['log_username']) { $randUser = $user1; }elseif ($user2 != $GLOBALS['log_username']) { $randUser = $user2; } if ($user1 == $GLOBALS['log_username'] && $user2 != $GLOBALS['log_username']) { $messages .= '<li class="row" id="pm_row_'.$id.'"><div class="me">'.$message.'</div></li>'; }else{ $messages .= '<li class="row" id="pm_row_'.$id.'">'.userImage($randUser,getAvatar($randUser),28,28,$link = true,$up = true).'<div class="userfrnd">'.$message.'</div></li>'; } } return $this->friend_username."$id|$messages|$randUser"; // this is for ^^^^^^^ testing purpose } } 

here is the other file where I am calling the other class method. And its content-type is text/event-stream

class update_chat extends SSEEvent { public function update(){ //Here's the place to send data $feed = new Feed(); return $feed->update_chat(); } public function check(){ //Here's the place to check when the data needs update return true; } } 

Any idea or suggestion why this problem persist ?

thanks in advance.

5
  • So, do you have any error? Commented Jun 26, 2015 at 2:23
  • no, thers is no error, its just returning empty string I think. Commented Jun 26, 2015 at 2:24
  • Is it possible that $row["username"] is empty? Commented Jun 26, 2015 at 2:25
  • no, $row["username"] is not empty, I am also using that and its working good. Commented Jun 26, 2015 at 2:26
  • the foo() method is returning its all values where I am using it. but its not returning its value in bar() since I call bar() in another file. Commented Jun 26, 2015 at 2:29

2 Answers 2

4

If you are calling bar() in another file and then creating a new Foo in otherClass, you are not referencing the same instance of Foo. Either make $friend_username static and call it statically

public static $friend_username; public function update(){ //Here's the place to send data return Foo::$friend_username; } 

or at least make the function static

public static function bar() {} public function update(){ //Here's the place to send data return Foo::bar(); } 

or pass in the instance of Foo to the function

public function update(Foo $Foo){ //Here's the place to send data return $Foo->bar(); } 

If you want to call a static method from within the same class, you have to use the self identifier (self::$var)

 class Feed { public static $friend_username = array(); // ONLINE FRIENDS LOGIC public function online_friends(){ $friendsHTML = ''; $countOnlineFriends = ''; if(GetFriends($GLOBALS['log_username']) != false) { $all_friends = GetFriends($GLOBALS['log_username']); $orLogic = ''; foreach($all_friends as $key => $user){ if(IsBlocked($GLOBALS['log_username'],$user,true) == false){ $orLogic .= "username='$user' OR "; } } $orLogic = chop($orLogic, "OR "); $sql = "SELECT username, avatar, logged_in FROM users WHERE ($orLogic) AND logged_in = 1"; $query = mysqli_query($GLOBALS['db_conx'], $sql); $friend_loggedIn = array(); while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) { array_push(self::$friend_username, $row["username"]); $friend_avatar = $row["avatar"]; $friend_loggedIn[] = $row["logged_in"]; $friend_pic = userImage(self::$friend_username,$friend_avatar,'42','42',$link = false,$up = true); $friendsHTML .= '<li><a href="#" onClick="chatbox(\''.self::$friend_username.'\',\''.getName(self::$friend_username,true).'\');return false;">'.$friend_pic.' '.getName(self::$friend_username,true).'</li>'; $countFriends = count($friend_loggedIn); $countOnlineFriends = ($countFriends > 0) ? '<span class="online_friends animated">'.$countFriends.'</span>' : ''; } }else{ $friendsHTML = 'No friends'; } return "$countOnlineFriends|$friendsHTML"; } public function update_chat() { $id = ''; $messages = ''; $randUser = ''; $user = Feed::$friend_username; foreach ($user as $key => $value) { $user[$key] = sanitize($value); } //I leave it up to you to figure out how you want to deal with the array of users in this next line $sql = "SELECT * FROM pm_chat WHERE (sender='$GLOBALS[log_username]' AND receiver='$user') OR (sender='$user' AND receiver='$GLOBALS[log_username]') ORDER BY datetime DESC"; $result = mysqli_query($GLOBALS['db_conx'],$sql) or die(mysqli_error($GLOBALS['db_conx'])); while ($row = mysqli_fetch_assoc($result)) { $id = $row['id']; $user1 = $row['sender']; $user2 = $row['receiver']; $message = parseData($row['message']); $did_read = $row['did_read']; $datetime = $row['datetime']; if ($user1 != $GLOBALS['log_username']) { $randUser = $user1; }elseif ($user2 != $GLOBALS['log_username']) { $randUser = $user2; } if ($user1 == $GLOBALS['log_username'] && $user2 != $GLOBALS['log_username']) { $messages .= '<li class="row" id="pm_row_'.$id.'"><div class="me">'.$message.'</div></li>'; }else{ $messages .= '<li class="row" id="pm_row_'.$id.'">'.userImage($randUser,getAvatar($randUser),28,28,$link = true,$up = true).'<div class="userfrnd">'.$message.'</div></li>'; } } return Feed::$friend_username."$id|$messages|$randUser"; // this is for ^^^^^^^ testing purpose } } 
Sign up to request clarification or add additional context in comments.

23 Comments

I also tried that to make $friend_username; static. but that is same too.
I don't understand of this line public function update(Foo $Foo) how this works? as argument Foo $Foo ?
Type hinting php.net/manual/en/language.oop5.typehinting.php Technically you could get away without specifying that the argument should be of the class Foo, but it's better to have it in there.
you can see I only make 1 instance of this class public function update(){ //Here's the place to send data $Foo = new Foo(); return $Foo->bar(); }
so it should return the bar() value
|
0

Well, since your are using the method mysqli_fetch_array, could it be that more than one element is returned and that the last one is empty?

BTW, I don't understand why you are making a single variable attribution inside a while statement. Supposedly, the last running (if some) will overwrite the variable's value.

Another observation, on the second code. If you are calling the bar() method right off the bat, shoudn't the variable be empty anyway? I understand that $friend_username is only assigned inside the foo() method.

3 Comments

no, that is not empty and not even the last element is empty.
and this is not single variable attribution in while, there is more. but I showed you in short to just demonstrate what is wrong with this. and what I am doing.
Alright. As I can only base myself for what I can see in your snippet of code, $Foo = new Foo(); return $Foo->bar(); , it really seems to me that the $friend_username is not assigned at the moment. Could you show more of what is happening?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.