i made this student class
<?php
Class Student
{
private $name;
private $amountOfGrades;
private $grades=array();
function __construct(string $name,int $amountOfGrades){
$this->name=$name;
$this->amountOfGrades=$amountOfGrades;
}
function setGrades(...$grade)
{
for($i=0;$i<$this->amountOfGrades;$i++)
$this->grades[$i]=$grade;
}
function getGrades(){
return $this->grades;
}
function getAvg(){
$sum=0;
for($i=0;$i<count($this->grades);$i++)
$sum+=$this->grades[$i];
return $sum/$amountOfGrades;
}
}
?>
and this is where i test it
<?php
require_once "studentClass.php";
$Jack = new Student("Jack",3);
$Jack->setGrades(100,50,69);
print_r($Jack->getAvg());
?>
the problem is that i keep getting this error
Fatal error: Uncaught TypeError: Unsupported operand types: array + null in D:\xamp\htdocs\studentClass.php:27 Stack trace: #0 D:\xamp\htdocs\testStudent.php(6): Student->getAvg() #1 {main} thrown in D:\xamp\htdocs\studentClass.php on line 27
how am i supposed to store the amount into sum to return the avg if i cant use +=
thanks in advance
Your setGrades was the problem, you passed the whole grade array to every single element. Also you have to use $this->amountOfGrades not $amountOfGrades. Take a look ;)
<?php
Class Student
{
private $name;
private $amountOfGrades;
private $grades=array();
function __construct(string $name,int $amountOfGrades){
$this->name=$name;
$this->amountOfGrades=$amountOfGrades;
}
function setGrades(...$grade)
{
for($i=0;$i<$this->amountOfGrades;$i++)
$this->grades[$i]=$grade[$i];
}
function getGrades(){
return $this->grades;
}
function getAvg(){
$sum=0;
for($i=0;$i<count($this->grades);$i++) {
$sum+=$this->grades[$i];
}
return $sum / $this->amountOfGrades;
}
}
$Jack = new Student("Jack",3);
$Jack->setGrades(100,50,69);
print_r($Jack->getAvg());
?>
Related
im writing a class in php and I am getting an error stating:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function StudentAccountInquiry::get_semester(), 0 passed in /home/cg/root/632dc36089450/main.php on line 28 and exactly 1 expected in /home/cg/root/632dc36089450/main.php:14
Stack trace:
#0 /home/cg/root/632dc36089450/main.php(28): StudentAccountInquiry->get_semester()
#1 {main}
thrown in /home/cg/root/632dc36089450/main.php on line 14
<?php
class StudentAccountInquiry{
public $semester;
public $balance;
function set_semester($semester){
$this->semester = $semester;
}
function set_balance($balance){
$this->balance = $balance;
}
function get_semester($semester){
return $this->semester;
}
function get_balance($balance){
return $this->balance;
}
}
$fall = new StudentAccountInquiry();
$fallBalance = new StudentAccountInquiry();
$fall->set_semester('Fall 2022');
$fallBalance->set_balance('$10,000');
echo $fall->get_semester();
echo $balance->get_balance();
?>
It looks to me that everything is there.
Please Advise, Thanks!
get_balance and get_balance function has function arguments $semester and $balance respectively , which you actually do not need, as you just returning class variable
so you can change for function definition to this
Old
function get_semester($semester){
return $this->semester;
}
function get_balance($balance){
return $this->balance;
}
New
function get_semester(){
return $this->semester;
}
function get_balance(){
return $this->balance;
}
Here I want to use the code class Child_Site extends Site to extend the class Site, and the class Site is used with the __construct to receive $par1, $par2. When I use
$D=new Child_Site("ddd","gggg");$D->getTitle(); to visit his parent class, it is totally fine. But when I want to visit the child method, why it give the error?
Fatal error: Uncaught ArgumentCountError: Too few arguments to
function Site::__construct(), 0 passed in
D:\phpstudy_pro\WWW\index.php on line 39 and exactly 2 expected in
D:\phpstudy_pro\WWW\index.php:7 Stack trace: #0
D:\phpstudy_pro\WWW\index.php(39): Site->__construct() #1 {main}
thrown in D:\phpstudy_pro\WWW\index.php on line 7
<?php
class Site {
var $url;
var $title;
function __construct( $par1, $par2 ) {
$this->url = $par1;
$this->title = $par2;
}
function setUrl($par){
$this->url = $par;
}
function getUrl(){
echo $this->url . PHP_EOL;
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title . PHP_EOL;
}
}
class Child_Site extends Site{
var $category;
function setCate($par){
$this->category=$par;
}
function getCate(){
echo $this->category.PHP_EOL;
}
}
$D = new Child_Site("ddd","gggg");
$D->getTitle();
$s = new Child_Site;
$s->setCate("xx");
$s->getCate();
?>
The url to JSON structure
http://api.giphy.com/v1/gifs/search?q=funny+cat&offset=100&limit=1&api_key=dc6zaTOxFJmzC
Now I'm trying to parse this Json data to my objects:
this is my class:
<?php
class MappedEntity{
private $id;
private $mp4Url;
public function setId($id){
$this->id=$id;
}
public function setMp4Url($mp4Url){
$this->mp4Url=$mp4Url;
}
public function getId(){
return $id;
}
public function getMp4Url(){
return $mp4Url;
}
function __contruct($jsonData){
foreach($jsonData as $key => $val){
if(property_exists(__CLASS__,$key)){
$this->$key = $val;
}
}
}
}
?>
the code thats calling the class:
<?php
require_once 'mappedEntity.php';
$keyword = $_POST["keyword"];
$url="http://api.giphy.com/v1/gifs/search?q=".$keyword."&offset=100&limit=1&api_key=dc6zaTOxFJmzC";
$json = file_get_contents($url);
$file = json_decode($json);
$entity = new MappedEntity($file);
echo $entity->getId();
?>
Im getting
Notice:
Undefined variable: id in C:...\MappedEntity.php on line 14
which is following ine
13 public function getId(){
14 return $id;
15 }
OK, I changed the getter methods, and constructor , now the error goes
Notice: Undefined variable: id in C:\....\MappedEntity.php on line 14
Fatal error: Cannot access empty property in C:...\MappedEntity.php on line 14
I assume , my mapping method inside of constructor is not working fine?
You're missing the $this on both getID and getMp4URL functions, they should read:
public function getId(){
return $this->id;
}
and
public function getMp4Url(){
return $this->mp4Url;
}
this is my first question
I Have following class
class ProDetection {
public function ProDetection ( ) { }
public function detect($word) {
............
}
public function getScore() {
return $score;
}
}
class SaveDetection {
public function SaveDetection($words) {
$proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
}
in other PHP file, i'm try to calling SaveDetection to getScore();
$konten = "xxxx xxxx xxx";
$save = new SaveDetection($konten);
print_r( $save->getScore() );
But i got an error message
Notice: Undefined property: SaveDetection::$proDetection in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Fatal error: Call to a member function getScore() on a non-object in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22
Please need your help
You never declare the the $proDetection member variable. Basically in your SaveDetection constructor you are declaring $proDetection as a local variable
class SaveDetection {
public function SaveDetection($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
EDIT:
PS. You should really use PHP's __construct() syntax instead of the old style of constructors. See here.
class SaveDetection {
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
Try this. Declare your variables as private (Coz if your code grows, it is hard to find what all the object or variables are used in the class.)
class SaveDetection {
private $proDetection = null;
public function __construct($words) {
$this->proDetection = new ProDetection();
for($i=0;$i<sizeof($words);$i++) {
$this->proDetection->detect($words[$i]);
}
}
public function getScore() {
return $this->proDetection->getScore();\\line 22
}
private $proDetection;
}
I'm building some classes for working with playing cards. I have a Card class and a Deck class. I want to implement drawing a card from the deck by using array_shift() on an array of Card objects; this array is a property of Deck. Here is the code for the classes, which is stored in the file "cardlib.php":
<?php
class Card
{
private $suit="";
private $face="";
function __construct($suit,$face){
$this->suit=$suit;
$this->face=$face;
}
public function getSuit(){
return $suit;
}
public function getFace(){
return $face;
}
public function display(){
echo $this->suit.$this->face;
}
}
class Deck
{
private $suits=array("S","H","C","D");
private $faces=array("2","3","4","5",
"6","7","8","9","10",
"J","Q","K","A");
private $stack=array();
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$stack[] = $card;
}
}
}
public function doShuffle(){
shuffle($this->stack);
}
public function draw(){
$card = array_shift($this->stack);
var_dump($card);
return $card;
}
}
?>
And here is the test code, in "index.php":
<?php
include_once "cardlib.php";
$myDeck=new Deck();
$myDeck->doshuffle();
$card=$myDeck->draw();
$card->display();
?>
The test code gives me the following error message:
NULL
Fatal error: Call to a member function display() on a non-object in C:\wamp\www\cardgames\index.php on line 6
It seems that array_shift() isn't returning the reference to the card object, or I'm not properly initializing the $card variable with what array_shift() returns. How do I get the object that I want?
In the constructor, you store the stack in a local variable. Use $this->stack to store it in the member variable.
function __construct(){
foreach ($this->suits as $suit){
foreach ($this->faces as $face){
$card = new Card($suit,$face);
$this->stack[] = $card;
}
}
}
In Deck::__construct(), use $this->stack[] = .. instead of $stack[] = ..