Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

casting object of closure to string?

Posted on 2014-12-28
6
Medium Priority
?
83 Views
Last Modified: 2014-12-29
Hello,

I have this function that returns an object, which i would like to turn into a string. Casting it returns an error. Please help?

$hehe =(string) (function() {
			echo '[';
			for ($j = 0; $j < 1; $j++) {
				echo '{';
				echo '"title":"All Day Event",';
				echo '"start":"2014-11-01",';
				echo '"end":"2014-11-01"';
				if ($j == 1-1){
			             echo '}';
			        }
               			else {
		        		echo '},';
			       	}

			}
			echo ']';
		});

Open in new window

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40521305
Where is the object that it returns?
0
 

Author Comment

by:NeverEndingFlashStories
ID: 40521319
Well, apparently when i do gettype($hehe)
$hehe is an object.

I kind of expected it to be a string.
By the way, my casting it as a (string) at the beginning returns an error
0
 
LVL 84

Expert Comment

by:ozo
ID: 40521345
What would you want the contents of the string to be?
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 

Author Comment

by:NeverEndingFlashStories
ID: 40521419
Ah i am trying to produce this :

$hehe =  '[
  {
    "title": "All Day Event",
    "start": "2014-11-01"
  },
  {
    "title": "Long Event",
    "start": "2014-11-07",
    "end": "2014-11-10"
  }]'

I just realized i may have used the wrong thing when i used echo...i should have used concatenate instead, maybe? I am new to php..
0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 40521572
Do you mean something like this?
$f = function($t,$s,$e){
            $str=      '[
';
            for ($j = 0; $j < 2; $j++) {
                  $str .= '  {
';
                  if ($j == 2-1){
                          $str .= '    "title":"'.$t.',
    "start": "'.$s.'",
    "end": "'.$e.'
  }';
                    }
                             else {
                        $str .= '    "title":"All Day Event",
';
                               $str .= '    "start":"2014-11-01",
';
                          $str .= '  },
';
                               }

                  }
                  $str .= ']';
             return $str;
};
$hehe=$f("Long Event","2014-11-07","2014-11-10");
echo $hehe;
0
 

Author Closing Comment

by:NeverEndingFlashStories
ID: 40523094
Yes ,

precisely. Thank you so much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question