我收到错误:
未定义的属性: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 :(得分:0)
您无法从外部访问类中的函数变量。
class stockdata{
public function data($ticker,$output){
....
$status = $result['status']['message']; // <--- this is not accessible.
}
您必须将其定义为公共财产:
class stockdata{
public $status; // definition.
public function data($ticker,$output){
....
$this->status = $result['status']['message']; // <-- Works!!
}
现在您可以访问它了。
$stock = new stockdata();
...
echo $stock->status;
答案 1 :(得分:0)
使用箭头操作符 - &gt;。
无法通过对象访问静态属性