PHP - How do I pass the output of variable->string() into variable = new ()

PHP - How do I pass the output: $os6x->toString() to: $os1 = new OSRef(651409.903, 313177.270);?

I'm using the PHPcoord package to try and convert a UK 6 figure Grid reference to Lat & Long.

According to the examples given in the link above. This is a two part process.

Part 1) Converting the 6 Figure Grid Reference into an OSGB grid reference.

ie: Convert TG514131 into (651400, 313100).

The example given is:

$os6 = "TG514131";
echo "Six figure string: " . $os6 . "<br />";
$os6x = getOSRefFromSixFigureReference($os6);
echo "Converted to OS Grid Ref: " . $os6x->toString() . " - " . $os6x->toSixFigureString();
Six figure string: TG514131
Converted to OS Grid Ref: (651400, 313100) - TG514131

Open in new window


Part 2) converts the OSGB reference into Lat & Long by:

$os1 = new OSRef(651409.903, 313177.270);
echo "OS Grid Reference: " . $os1->toString() . " - " . $os1->toSixFigureString() . "<br />";
$ll1 = $os1->toLatLng();
echo "Converted to Lat/Long: " . $ll1->toString();

Open in new window


The problem I am having is that I can't get the output of stage 1:

$os6x->toString()

Open in new window

into the input of stage 2:

$os1 = new OSRef(651409.903, 313177.270);
I've tried converting $os6x->toString() into a variable and then using:

$myvariable = $os6x->toString();
$os1 = new OSRef($myvariable);

Open in new window


or

$os1 = new OSRef$myvariable;

Open in new window


or

$os1 = new OSRef . $myvariable;

Open in new window


and unsurprisingly, none work.

As you can tell I'm not very good at this and am struggling to find any help on Google.

My next step would be to pattern match $myvariable and then split it into two variables and plug them in like this:

$os1 = new OSRef($number1, $number2);

Open in new window


But I'm sure this is not the correct way to achieve this and I'm missing something very simple.
LVL 1
LetterpartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skullnobrainsCommented:
if the tostring method provides those numbers, they are probably stored in separate attributes
use var_dump ( get_object_vars ($yourobject) ) to findout what attribute holds the number you need
1
kenfcampCommented:
"IF" I'm understanding what you're using, and what you're trying to accomplish correctly (and I could need more information), you can't

The "OSRef" is a grid reference and can't be converted to latitude/longitude

What is it you're trying to do exactly
0
LetterpartAuthor Commented:
Hi @skullnobrains

var_dump(get_object_vars($os6x));

Open in new window


gives me:

C:\wamp64\www\mrt\index.php:81:
array (size=2)
  'easting' => int 292900
  'northing' => float 668000

Open in new window


I'm now struggling to get these values out so I can use them in the second stage (also not sure why one is an int and the other a float as both look like integers to me).

@kenfcamp

Yes. I'm trying to convert a 6 figure grid reference to lat/long so I can then plot markers on a Google Map. It is possible. Ordnance Survey have provided the workings for it (Annex C ) and the PHPcoord package does all the calculations for me. I just need to put the two stages together with my very limited knowledge & skills in PHP.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Chris StanyonWebDevCommented:
Your call to the getOSRefFromSixFigureReference() returns an instance of the OSRef class. This class has 2 properties - easting and northing, so to get at those 2 values you would access the properties:

$os6x->easting;
$os6x->northing;

In your question, you look like you're trying to access those 2 properties so you can create a new OSRef object. That's not needed because $os6x is already an instance of the OSRef class, so you can already call the various methods on it:

echo $os6x->toLatLng();
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kenfcampCommented:
Is this what you're looking for?

$os6 = "TG514131";
echo "Six figure string: " . $os6 . "<br />";
$os6x = getOSRefFromSixFigureReference($os6);
echo "Converted to OS Grid Ref: " . $os6x->toString() . " - " . $os6x->toSixFigureString();


$os1 = getOSRefFromSixFigureReference($os6);
#$os1 = new OSRef(651409.903, 313177.270);
echo "\n";
echo "OS Grid Reference: " . $os1->toString() . " - " . $os1->toSixFigureString() . "<br />";
echo "\n";
$ll1 = $os1->toLatLng();


// Uncomment to use WGS84
#$ll1->OSGB36ToWGS84();

echo "Converted to Lat/Long: " . $ll1->toString();

Open in new window


Ken
0
Chris StanyonWebDevCommented:
Actually, the last part of my script probably won't work (the echo part). Calling toLatLng() on an OSRef object will return a new LatLng object, which has 2 properties: lat, lng, so you would probably want something like:

$os6 = "TG514131";
$os6x = getOSRefFromSixFigureReference($os6);
$myLatLng = $os6x->toLatLng();
echo $myLatLng->lat;
echo $myLatLng->lng;

Open in new window

0
LetterpartAuthor Commented:
@Chris Stanyon

Thank you. I had been all over Google trying to work this out with no luck and it was "so simple". always is when you know the answer.

I've now past this into Lat Long and have my values ready to populate my GeoJSON file and ultimately the Google Map.
0
Chris StanyonWebDevCommented:
Good stuff :)

Doesn't seem to be a whole lot of documentation on it, so it's a case of scanning the source code on GitHub to figure things out.
0
LetterpartAuthor Commented:
Thanks for your help. This is for a charity and they will be chuffed with the finished project.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Google

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.