Use variable outside class/function












0















I'm trying to use the $rank variable outside the function and class.



class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = '******';
protected static $ServiceURI = "/api";
protected static $ServiceRegion = "us-west-1";
protected static $ServiceName = "awis";


public function __construct($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
$now = time();
$this->amzDate = gmdate("YmdTHisZ", $now);
$this->dateStamp = gmdate("Ymd", $now);

}

/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$canonicalQuery = $this->buildQueryParams();
$canonicalHeaders = $this->buildHeaders(true);
$signedHeaders = $this->buildHeaders(false);
$payloadHash = hash('sha256', "");
$canonicalRequest = "GET" . "n" . self::$ServiceURI . "n" . $canonicalQuery . "n" . $canonicalHeaders . "n" . $signedHeaders . "n" . $payloadHash;
$algorithm = "AWS4-HMAC-SHA256";
$credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
$stringToSign = $algorithm . "n" . $this->amzDate . "n" . $credentialScope . "n" . hash('sha256', $canonicalRequest);
$signingKey = $this->getSignatureKey();
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
$authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;

$url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
$ret = self::makeRequest($url, $authorizationHeader);
// echo "nResults for " . $this->site .":nn";
// echo $ret;
self::parseResponse($ret);
}

protected function sign($key, $msg) {
return hash_hmac('sha256', $msg, $key, true);
}

protected function getSignatureKey() {
$kSecret = 'AWS4' . $this->secretAccessKey;
$kDate = $this->sign($kSecret, $this->dateStamp);
$kRegion = $this->sign($kDate, self::$ServiceRegion);
$kService = $this->sign($kRegion, self::$ServiceName);
$kSigning = $this->sign($kService, 'aws4_request');
return $kSigning;
}

/**
* Builds headers for the request to AWIS.
* @return String headers for the request
*/
protected function buildHeaders($list) {
$params = array(
'host' => self::$ServiceEndpoint,
'x-amz-date' => $this->amzDate
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
if ($list)
$keyvalue = $k . ':' . $v;
else {
$keyvalue = $k;
}
}
return ($list) ? implode("n",$keyvalue) . "n" : implode(';',$keyvalue) ;
}

/**
* Builds query parameters for the request to AWIS.
* Parameter names will be in alphabetical order and
* parameter values will be urlencoded per RFC 3986.
* @return String query parameters for the request
*/
protected function buildQueryParams() {
$params = array(
'Action' => self::$ActionName,
'Count' => self::$NumReturn,
'ResponseGroup' => self::$ResponseGroupName,
'Start' => self::$StartNum,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue = $k . '=' . rawurlencode($v);
}
return implode('&',$keyvalue);
}

/**
* Makes request to AWIS
* @param String $url URL to make request to
* @param String authorizationHeader Authorization string
* @return String Result of request
*/
protected function makeRequest($url, $authorizationHeader) {
// echo "nMaking request to:n$urln";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/xml',
'Content-Type: application/xml',
'X-Amz-Date: ' . $this->amzDate,
'Authorization: ' . $authorizationHeader
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/**
* Parses XML response from AWIS and displays selected data
* @param String $response xml response from AWIS
*/

public static function parseResponse($response) {
$xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
$info = $xml->Response->UrlInfoResult->Alexa;
$links = $info->ContentData->LinksInCount;
$rank = $info->TrafficData->Rank;
echo "<br>Links in Count: " .$links;
echo "<br>Rank: " .$rank;

);
}
}
}


}



Does anyone have an idea?
Thanks!



I want to add the $rank to an array;



      $arrayinfo = array(
"domain" => $ip,
"ip" => $ipphp,
"Rank" => $rank,);









share|improve this question

























  • You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

    – Thomas Lefetz
    Nov 21 '18 at 12:07











  • Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

    – The fourth bird
    Nov 21 '18 at 12:14













  • I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

    – Weazly
    Nov 21 '18 at 12:41
















0















I'm trying to use the $rank variable outside the function and class.



class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = '******';
protected static $ServiceURI = "/api";
protected static $ServiceRegion = "us-west-1";
protected static $ServiceName = "awis";


public function __construct($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
$now = time();
$this->amzDate = gmdate("YmdTHisZ", $now);
$this->dateStamp = gmdate("Ymd", $now);

}

/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$canonicalQuery = $this->buildQueryParams();
$canonicalHeaders = $this->buildHeaders(true);
$signedHeaders = $this->buildHeaders(false);
$payloadHash = hash('sha256', "");
$canonicalRequest = "GET" . "n" . self::$ServiceURI . "n" . $canonicalQuery . "n" . $canonicalHeaders . "n" . $signedHeaders . "n" . $payloadHash;
$algorithm = "AWS4-HMAC-SHA256";
$credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
$stringToSign = $algorithm . "n" . $this->amzDate . "n" . $credentialScope . "n" . hash('sha256', $canonicalRequest);
$signingKey = $this->getSignatureKey();
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
$authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;

$url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
$ret = self::makeRequest($url, $authorizationHeader);
// echo "nResults for " . $this->site .":nn";
// echo $ret;
self::parseResponse($ret);
}

protected function sign($key, $msg) {
return hash_hmac('sha256', $msg, $key, true);
}

protected function getSignatureKey() {
$kSecret = 'AWS4' . $this->secretAccessKey;
$kDate = $this->sign($kSecret, $this->dateStamp);
$kRegion = $this->sign($kDate, self::$ServiceRegion);
$kService = $this->sign($kRegion, self::$ServiceName);
$kSigning = $this->sign($kService, 'aws4_request');
return $kSigning;
}

/**
* Builds headers for the request to AWIS.
* @return String headers for the request
*/
protected function buildHeaders($list) {
$params = array(
'host' => self::$ServiceEndpoint,
'x-amz-date' => $this->amzDate
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
if ($list)
$keyvalue = $k . ':' . $v;
else {
$keyvalue = $k;
}
}
return ($list) ? implode("n",$keyvalue) . "n" : implode(';',$keyvalue) ;
}

/**
* Builds query parameters for the request to AWIS.
* Parameter names will be in alphabetical order and
* parameter values will be urlencoded per RFC 3986.
* @return String query parameters for the request
*/
protected function buildQueryParams() {
$params = array(
'Action' => self::$ActionName,
'Count' => self::$NumReturn,
'ResponseGroup' => self::$ResponseGroupName,
'Start' => self::$StartNum,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue = $k . '=' . rawurlencode($v);
}
return implode('&',$keyvalue);
}

/**
* Makes request to AWIS
* @param String $url URL to make request to
* @param String authorizationHeader Authorization string
* @return String Result of request
*/
protected function makeRequest($url, $authorizationHeader) {
// echo "nMaking request to:n$urln";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/xml',
'Content-Type: application/xml',
'X-Amz-Date: ' . $this->amzDate,
'Authorization: ' . $authorizationHeader
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/**
* Parses XML response from AWIS and displays selected data
* @param String $response xml response from AWIS
*/

public static function parseResponse($response) {
$xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
$info = $xml->Response->UrlInfoResult->Alexa;
$links = $info->ContentData->LinksInCount;
$rank = $info->TrafficData->Rank;
echo "<br>Links in Count: " .$links;
echo "<br>Rank: " .$rank;

);
}
}
}


}



Does anyone have an idea?
Thanks!



I want to add the $rank to an array;



      $arrayinfo = array(
"domain" => $ip,
"ip" => $ipphp,
"Rank" => $rank,);









share|improve this question

























  • You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

    – Thomas Lefetz
    Nov 21 '18 at 12:07











  • Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

    – The fourth bird
    Nov 21 '18 at 12:14













  • I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

    – Weazly
    Nov 21 '18 at 12:41














0












0








0








I'm trying to use the $rank variable outside the function and class.



class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = '******';
protected static $ServiceURI = "/api";
protected static $ServiceRegion = "us-west-1";
protected static $ServiceName = "awis";


public function __construct($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
$now = time();
$this->amzDate = gmdate("YmdTHisZ", $now);
$this->dateStamp = gmdate("Ymd", $now);

}

/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$canonicalQuery = $this->buildQueryParams();
$canonicalHeaders = $this->buildHeaders(true);
$signedHeaders = $this->buildHeaders(false);
$payloadHash = hash('sha256', "");
$canonicalRequest = "GET" . "n" . self::$ServiceURI . "n" . $canonicalQuery . "n" . $canonicalHeaders . "n" . $signedHeaders . "n" . $payloadHash;
$algorithm = "AWS4-HMAC-SHA256";
$credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
$stringToSign = $algorithm . "n" . $this->amzDate . "n" . $credentialScope . "n" . hash('sha256', $canonicalRequest);
$signingKey = $this->getSignatureKey();
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
$authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;

$url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
$ret = self::makeRequest($url, $authorizationHeader);
// echo "nResults for " . $this->site .":nn";
// echo $ret;
self::parseResponse($ret);
}

protected function sign($key, $msg) {
return hash_hmac('sha256', $msg, $key, true);
}

protected function getSignatureKey() {
$kSecret = 'AWS4' . $this->secretAccessKey;
$kDate = $this->sign($kSecret, $this->dateStamp);
$kRegion = $this->sign($kDate, self::$ServiceRegion);
$kService = $this->sign($kRegion, self::$ServiceName);
$kSigning = $this->sign($kService, 'aws4_request');
return $kSigning;
}

/**
* Builds headers for the request to AWIS.
* @return String headers for the request
*/
protected function buildHeaders($list) {
$params = array(
'host' => self::$ServiceEndpoint,
'x-amz-date' => $this->amzDate
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
if ($list)
$keyvalue = $k . ':' . $v;
else {
$keyvalue = $k;
}
}
return ($list) ? implode("n",$keyvalue) . "n" : implode(';',$keyvalue) ;
}

/**
* Builds query parameters for the request to AWIS.
* Parameter names will be in alphabetical order and
* parameter values will be urlencoded per RFC 3986.
* @return String query parameters for the request
*/
protected function buildQueryParams() {
$params = array(
'Action' => self::$ActionName,
'Count' => self::$NumReturn,
'ResponseGroup' => self::$ResponseGroupName,
'Start' => self::$StartNum,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue = $k . '=' . rawurlencode($v);
}
return implode('&',$keyvalue);
}

/**
* Makes request to AWIS
* @param String $url URL to make request to
* @param String authorizationHeader Authorization string
* @return String Result of request
*/
protected function makeRequest($url, $authorizationHeader) {
// echo "nMaking request to:n$urln";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/xml',
'Content-Type: application/xml',
'X-Amz-Date: ' . $this->amzDate,
'Authorization: ' . $authorizationHeader
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/**
* Parses XML response from AWIS and displays selected data
* @param String $response xml response from AWIS
*/

public static function parseResponse($response) {
$xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
$info = $xml->Response->UrlInfoResult->Alexa;
$links = $info->ContentData->LinksInCount;
$rank = $info->TrafficData->Rank;
echo "<br>Links in Count: " .$links;
echo "<br>Rank: " .$rank;

);
}
}
}


}



Does anyone have an idea?
Thanks!



I want to add the $rank to an array;



      $arrayinfo = array(
"domain" => $ip,
"ip" => $ipphp,
"Rank" => $rank,);









share|improve this question
















I'm trying to use the $rank variable outside the function and class.



class UrlInfo {
protected static $ActionName = 'UrlInfo';
protected static $ResponseGroupName = 'Rank,LinksInCount';
protected static $ServiceHost = 'awis.amazonaws.com';
protected static $ServiceEndpoint = 'awis.us-west-1.amazonaws.com';
protected static $NumReturn = 10;
protected static $StartNum = 1;
protected static $SigVersion = '2';
protected static $HashAlgorithm = '******';
protected static $ServiceURI = "/api";
protected static $ServiceRegion = "us-west-1";
protected static $ServiceName = "awis";


public function __construct($accessKeyId, $secretAccessKey, $site) {
$this->accessKeyId = $accessKeyId;
$this->secretAccessKey = $secretAccessKey;
$this->site = $site;
$now = time();
$this->amzDate = gmdate("YmdTHisZ", $now);
$this->dateStamp = gmdate("Ymd", $now);

}

/**
* Get site info from AWIS.
*/
public function getUrlInfo() {
$canonicalQuery = $this->buildQueryParams();
$canonicalHeaders = $this->buildHeaders(true);
$signedHeaders = $this->buildHeaders(false);
$payloadHash = hash('sha256', "");
$canonicalRequest = "GET" . "n" . self::$ServiceURI . "n" . $canonicalQuery . "n" . $canonicalHeaders . "n" . $signedHeaders . "n" . $payloadHash;
$algorithm = "AWS4-HMAC-SHA256";
$credentialScope = $this->dateStamp . "/" . self::$ServiceRegion . "/" . self::$ServiceName . "/" . "aws4_request";
$stringToSign = $algorithm . "n" . $this->amzDate . "n" . $credentialScope . "n" . hash('sha256', $canonicalRequest);
$signingKey = $this->getSignatureKey();
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
$authorizationHeader = $algorithm . ' ' . 'Credential=' . $this->accessKeyId . '/' . $credentialScope . ', ' . 'SignedHeaders=' . $signedHeaders . ', ' . 'Signature=' . $signature;

$url = 'https://' . self::$ServiceHost . self::$ServiceURI . '?' . $canonicalQuery;
$ret = self::makeRequest($url, $authorizationHeader);
// echo "nResults for " . $this->site .":nn";
// echo $ret;
self::parseResponse($ret);
}

protected function sign($key, $msg) {
return hash_hmac('sha256', $msg, $key, true);
}

protected function getSignatureKey() {
$kSecret = 'AWS4' . $this->secretAccessKey;
$kDate = $this->sign($kSecret, $this->dateStamp);
$kRegion = $this->sign($kDate, self::$ServiceRegion);
$kService = $this->sign($kRegion, self::$ServiceName);
$kSigning = $this->sign($kService, 'aws4_request');
return $kSigning;
}

/**
* Builds headers for the request to AWIS.
* @return String headers for the request
*/
protected function buildHeaders($list) {
$params = array(
'host' => self::$ServiceEndpoint,
'x-amz-date' => $this->amzDate
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
if ($list)
$keyvalue = $k . ':' . $v;
else {
$keyvalue = $k;
}
}
return ($list) ? implode("n",$keyvalue) . "n" : implode(';',$keyvalue) ;
}

/**
* Builds query parameters for the request to AWIS.
* Parameter names will be in alphabetical order and
* parameter values will be urlencoded per RFC 3986.
* @return String query parameters for the request
*/
protected function buildQueryParams() {
$params = array(
'Action' => self::$ActionName,
'Count' => self::$NumReturn,
'ResponseGroup' => self::$ResponseGroupName,
'Start' => self::$StartNum,
'Url' => $this->site
);
ksort($params);
$keyvalue = array();
foreach($params as $k => $v) {
$keyvalue = $k . '=' . rawurlencode($v);
}
return implode('&',$keyvalue);
}

/**
* Makes request to AWIS
* @param String $url URL to make request to
* @param String authorizationHeader Authorization string
* @return String Result of request
*/
protected function makeRequest($url, $authorizationHeader) {
// echo "nMaking request to:n$urln";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/xml',
'Content-Type: application/xml',
'X-Amz-Date: ' . $this->amzDate,
'Authorization: ' . $authorizationHeader
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

/**
* Parses XML response from AWIS and displays selected data
* @param String $response xml response from AWIS
*/

public static function parseResponse($response) {
$xml = new SimpleXMLElement($response,LIBXML_ERR_ERROR,false,'http://awis.amazonaws.com/doc/2005-07-11');
if($xml->count() && $xml->Response->UrlInfoResult->Alexa->count()) {
$info = $xml->Response->UrlInfoResult->Alexa;
$links = $info->ContentData->LinksInCount;
$rank = $info->TrafficData->Rank;
echo "<br>Links in Count: " .$links;
echo "<br>Rank: " .$rank;

);
}
}
}


}



Does anyone have an idea?
Thanks!



I want to add the $rank to an array;



      $arrayinfo = array(
"domain" => $ip,
"ip" => $ipphp,
"Rank" => $rank,);






php class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 12:37







Weazly

















asked Nov 21 '18 at 12:04









WeazlyWeazly

34




34













  • You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

    – Thomas Lefetz
    Nov 21 '18 at 12:07











  • Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

    – The fourth bird
    Nov 21 '18 at 12:14













  • I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

    – Weazly
    Nov 21 '18 at 12:41



















  • You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

    – Thomas Lefetz
    Nov 21 '18 at 12:07











  • Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

    – The fourth bird
    Nov 21 '18 at 12:14













  • I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

    – Weazly
    Nov 21 '18 at 12:41

















You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

– Thomas Lefetz
Nov 21 '18 at 12:07





You should use the notion of singleton. You create on class UrlInfo and you only use this instance. Or you can use global variables, sessions, but i think this is not the good way.

– Thomas Lefetz
Nov 21 '18 at 12:07













Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

– The fourth bird
Nov 21 '18 at 12:14







Perhaps return $info->TrafficData->Rank; from the function? Or return an array with the values from the function and then do the echo part based on what is in the array.

– The fourth bird
Nov 21 '18 at 12:14















I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

– Weazly
Nov 21 '18 at 12:41





I need to add the $rank variable to an array ( i edited my question ). I tried to make it a global variable, but its not working.

– Weazly
Nov 21 '18 at 12:41












1 Answer
1






active

oldest

votes


















0














What if you declare $rank as a static variable like the $name of following example



<?php
class Test{
static $name;
static function setName($name){
self::$name=$name;
}

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name





share|improve this answer
























  • Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

    – Weazly
    Nov 21 '18 at 13:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411654%2fuse-variable-outside-class-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














What if you declare $rank as a static variable like the $name of following example



<?php
class Test{
static $name;
static function setName($name){
self::$name=$name;
}

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name





share|improve this answer
























  • Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

    – Weazly
    Nov 21 '18 at 13:05
















0














What if you declare $rank as a static variable like the $name of following example



<?php
class Test{
static $name;
static function setName($name){
self::$name=$name;
}

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name





share|improve this answer
























  • Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

    – Weazly
    Nov 21 '18 at 13:05














0












0








0







What if you declare $rank as a static variable like the $name of following example



<?php
class Test{
static $name;
static function setName($name){
self::$name=$name;
}

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name





share|improve this answer













What if you declare $rank as a static variable like the $name of following example



<?php
class Test{
static $name;
static function setName($name){
self::$name=$name;
}

}
Test::setName("Test Name");
Test::setName("Test1 Name");
echo Test::$name; //Test1 Name






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 12:19









Mithu CNMithu CN

466310




466310













  • Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

    – Weazly
    Nov 21 '18 at 13:05



















  • Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

    – Weazly
    Nov 21 '18 at 13:05

















Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

– Weazly
Nov 21 '18 at 13:05





Thanks! this works. But to put it in an array you have to put (string) in front 'Test::$name'. Like this (string)Test::$name

– Weazly
Nov 21 '18 at 13:05




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411654%2fuse-variable-outside-class-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules