2016-12-31 84 views
-1

我得到一個錯誤:未定義的屬性在PHP

Undefined property: stockdata::$status

在我的代碼

和我似乎無法找到問題。

錯誤發生在代碼的第一個鏈接上的第101行。我試圖創建一個類的新實例並使用該類的變量。從那我得到'未定義的屬性錯誤'。

這是我的HTML文件:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <link rel="stylesheet" href="./style.css"> 
    <!-- Required meta tags always come first --> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
    <meta http-equiv="x-ua-compatible" content="ie=edge"> 

    <!-- Bootstrap CSS --> 
<link rel="stylesheet" type="text/css" href="/style.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous"> 

    </head> 
    <body> 
<nav class="navbar"> 
    <a class="navbar-brand">Testing</a> 
    <ul class="nav navbar-nav"> 
     <li class="nav-item active"> 
      <a class="nav-link" href="#">Home</a></li> 
     <li class="nav-item"> 
      <a class="nav-link" href="#">Blog</a></li> 
     <li class="nav-item"> 
      <a class="nav-link 420" href="#">Etc</a></li> 
    </ul> 
</nav> 


<div class="container"> 
<form action "" method="post"> 
    <div class="form-group"> 
     <div class=""> 
      <input type="user" class="form-control-sm" id="user" name="user" placeholder="username">  
     </div> 
     <div class=""> 
      <input type="ticker" class="form-control-sm" id="ticker" name="ticker" placeholder="ticker"> 
     </div> 
     <div class=""> 
      <input type="quantity" class="form-control-sm" id="quantity" name="quantity" placeholder="quantity"> 
     </div> 
    <!-- 
    <div class="btn-group"> 
     <label for="message" class=""></label> 
     <button class="btn" type="button" data-toggle="dropdown"> 
     Asset Type</button> 

    <div class="dropdown-menu" aria-labelledby="dropdownMenu2"> 
    <button class="dropdown-item" type="button">BTC/ALT</button> 
    <button class="dropdown-item" type="button">US-Market</button> 
    <button class="dropdown-item" type="button">FOREX</button> 
    </div> 
    --> 
     <div class=""> 
      <input id="submit" name="submit" type="submit" class="btn btn-primary"> 
     </div> 
    </div> 
    </form> 
      <form action "" method="post"> 
      <input type="ticker2" class="form-control-sm" id="ticker2" name="ticker2" placeholder="ticker"> 
      <input id="submit2" name="submit2" type="submit" class="btn btn-primary"> 
      </form> 
</div> 

</body> 

這是我的PHP文件:

<?php 
include 'stockdata.php'; 

$servername = "localhost"; 
$username = "admin"; 
$password = "password"; 
$dbname = "db"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
/* 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
else{ 
    */ 

/* 
    $user = filter_input(INPUT_POST, 'user', FILTER_SANITIZE_SPECIAL_CHARS); 
    $quantity = filter_input(INPUT_POST, 'user', FILTER_SANITIZE_SPECIAL_CHARS); 
    $ticker = filter_input(INPUT_POST, 'user', FILTER_SANITIZE_SPECIAL_CHARS); 
*/ 


if (isset($_POST['submit'])){ 
    $user = $_POST['user']; 
    $quantity = $_POST['quantity']; 
    $ticker = $_POST['ticker']; 


    $d4e = new stockdata; 
    $d4e -> data($ticker,0); 
    $d4e->status; 
/* 
    var_dump($dataforentry->stockarray) <- This line is trash 


    var_dump($d4e); 

    */ 
    $sql = "INSERT INTO info (
    username, ticker, quantity) 
    VALUES ('$user','$ticker','$quantity') 
    ON DUPLICATE KEY UPDATE quantity=quantity+$quantity"; 

if ($conn->query($sql)==TRUE) 
{ 
    echo "New record created successfully"; 
} 
else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 
} 

if(isset($_POST['submit2'])){ 
    $ticker2 = $_POST['ticker2']; 
    $guy = new stockdata; 
    $guy -> data($ticker2,1); 
} 

/* 
$guy = new stockdata; 
$guy->data('AAPL'); 

*/ 
?> 
    <!-- jQuery first, then Tether, then Bootstrap JS. --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script> 
    </body> 
</html> 

<?php 

/* 
$ondemand = new SoapClient('http://ondemand.websol.barchart.com/service?wsdl'); 

$params = array(
    'symbols' => 'AAPL,GOOG', 
    'fields' => 'fiftyTwoWkHigh,fiftyTwoWkHighDate,fiftyTwoWkLow,fiftyTwoWkLowDate', 
    'mode' => 'I', 
); 

$result = $ondemand->getQuote($params); 

echo $result; 
*/ 
class stockdata{ 

public static function data($ticker,$output){ 

$get = file_get_contents('http://marketdata.websol.barchart.com/getQuote.json?key=85f03afeb0ebfb059b8e1812c74ff101&symbols=' . $ticker); 
$result = json_decode($get,true); 

$status = $result['status']['message']; 
$exchange = $result['results'][0]['exchange']; 
$symbol = $result['results'][0]['symbol']; 
$lastprice = $result['results'][0]['lastPrice']; 
$volume = $result['results'][0]['volume']; 
$percentchange = $result['results'][0]['percentChange']; 

$stockarray = array($status, $exchange, $symbol, $lastprice, $volume); 
/* 
class output extends stockdata{ 
public function data_out($ticker2){ 
} 
These two functions should be separate. Idea: Use $stockarray to pass the information. 

*/ 

if($output==1){ 
    echo "Output is 1 (function with output) <br />"; 
    echo $status . "<br />"; 
    echo $exchange . " | " . $symbol . "<br />"; 
    echo "Last Price : " . $lastprice . "<br />"; 
    echo "Volume : " . $volume . "<br />"; 
    echo "Percent Change : " . $percentchange . "<br />"; 
    } 
    else { 
      echo "Output is 0 (function no output) <br />";} 
    } 
} 
?> 

我已搜查和調試這個問題很長一段時間。我真的很感激任何幫助。

+0

郵政相關的代碼直接在這裏,作爲文本。 – Carcigenicate

+0

併發布代碼stockdata類(請不要pastebin) – scaisEdge

+0

我無法弄清楚如何在這裏放置一個大的代碼塊,因爲我的代碼的每一行都沒有空格。 – dg4e

回答

0

您無法從外部訪問類內部的函數變量。

class stockdata{ 

    public function data($ticker,$output){ 
     .... 
     $status = $result['status']['message']; // <--- this is not accessible. 
    } 

你必須將它定義爲一個公共屬性:現在

​​

您可以訪問它。

$stock = new stockdata(); 
... 
echo $stock->status; 
+0

你不能使用帶有靜態鍵的函數$ this-> status!你會得到一個錯誤,例如當不在對象上下文中使用$ this ...並且使用這個$ stock-> status時,他將返回public $ status的值而不是$ this-> status! – Basti

+0

@Basti謝謝你,我沒注意到。我更新了。 – Ibu