PHP Classes

Working on an assignment and don’t know where I am going wrong. Here is the assignment.

/* Step 1:

  • To get stated with this assignment, first create a new class called “Student”.
  • Inside your class, create a public variable called $grades.
  • Then create a private variable called $average.
  • Additionally, create a public method called CreateAverage().
  • Inside your method, you’ll set your $average attribute to equal the following formula:
  • array_sum($this->grades) / count($this->grades)
  • Now, inside your CreateAverage method, return the value of the $average attribute

Note: You wont need to change the class you created in Step 1 beyond this point.
*

  • Now that you have created your class and its method, I’d like you to
  • instantiate your class inside a variable called $jack.
  • Now, I’d like you to set the value of jack’s $grades attribute
  • to the following array of grades: array(98,95,85,79,90,92,91).
  • Remember that we are setting this attribute value outside of the class.
  • Now that you’ve set the grades for this student,
  • you can call the CreateAverage() method.
  • To output the method’s calculations,
  • you’ll need to echo the method since it returns a value.
  • Hint: Your final output for this assignment should be “90”.

Here is what I have so far:
[php]

<?php class Student { public $grades; private $average; public function CreateAverage() { $this->average = array_sum($this->grades) / count($this->grades); } } $jack = new Student; $jack->grades = array('98, 95, 85, 79, 90, 92, 91'); $jack->CreateAverage(); echo $jack->$grades; ?>[/php]

Read the directions again. You are literally getting step by step directions. You just need to follow them.

[php]<<?php
class Student
{
function CreateAverage()
{
$grades = array(98, 95, 85, 79, 90, 92, 91);
$average = array_sum($grades) / count($grades);
return $average;
}
}

$jack = new Student;
echo $jack->CreateAverage();
?>[/php]This should get you on your way.

Step by step instructions are given, I really don’t think they need code, and that code is still incorrect.

I just fixed an error. Code gives correct result of 90, just not exactly the way they outlined how to get it.

Instructions say “you’ll need to echo the method since it returns a value.” yet tells you to echo it. Beuler?..Beuler?..Beuler?

/* Step 1: * To get stated with this assignment, first create a new class called "Student". * Inside your class, create a public variable called $grades. * Then create a private variable called $average. * * Additionally, create a public method called CreateAverage(). * Inside your method, you'll set your $average attribute to equal the following formula: * array_sum($this->grades) / count($this->grades) [b]* Now, inside your CreateAverage method, return the value of the $average attribute[/b]

Note: You wont need to change the class you created in Step 1 beyond this point.
*

  • Now that you have created your class and its method, I’d like you to
  • instantiate your class inside a variable called $jack.
  • Now, I’d like you to set the value of jack’s $grades attribute
  • to the following array of grades: array(98,95,85,79,90,92,91).
  • Remember that we are setting this attribute value outside of the class.
  • Now that you’ve set the grades for this student,
  • you can call the CreateAverage() method.
  • To output the method’s calculations,
    * you’ll need to echo the method since it returns a value.

Is this not how it should work?

It will work with echo OR return. As you see, I used return, result is correctly 90. Part of his problem is the single quotes in his array. Depending on how you use it, echo may not work right. Dont remember off hand what the particular situation is.

  • Edit: Ok I remember, if echo, you are outputting the result, return is if you were going to use the value somehow.

That is one part. Not returning anything is another. Echoing the grades and not the method is the other.

Your code doesn’t help them though. You moved the array into the class. And the instructions state to echo the method, not for the method to echo the results.

As I said in my first response, it was to get him on his way with the correct result, not the answer to the instructions. The immediate problem he had was the quotes in the array. Also, as good as I am procedural I am that bad in oop. Until a week ago I thought a constructor was the company that builds houses or buildings. :o

I believe that is one of the problems with php. I thought I was doing it the oop way until I started working under a very senior architect that showed me I was still doing procedural, just with classes. It’s a hard switch to flip and a hard paradigm to actually understand when used to the procedural way of doing things.

I need to install 7, but judging from the change log, I just don’t see that much of a difference, yet.

[member=72272]astonecipher[/member],

How is this per the instructions? (Seriously dont know much more oop than this :()

[php]class Student
{
public $grades;
private $average;

function CreateAverage($grades)
    {
    $average = array_sum($grades) / count($grades);
    echo $average;
    }
}

$grades = array(98, 95, 85, 79, 90, 92, 91);
$jack = new Student;
echo $jack->CreateAverage($grades);[/php]

Technically, your version is better because of the dependency injection (should include type hinting, but that isn’t a requirement.) If not using type hinting, out of the scope for this assignment, you should verify that the parameter is an array and they are numeric.

[php]public function CreateAverage()
{
$this->average = array_sum($this->grades) / count($this->grades);
return $this->average;
}[/php]

Here is what I hear you saying, 我不知道你在说什么. So far,I do just fine without OOP. I take a nibble here and there. It took me years just to grasp methods and properties are just variables and functions? Like WTF? Why rename something that already has a name and is understood. You say type hinting, I hear autocomplete or auto suggest like when you type in the google search. As I understand it, Inheritance is when a rich uncle leaves you money when he dies.

Wow, boys, that was a fun read… LOL

Kevin, I get the feeling you know more than you are letting on…

Here is a better example:

[php]<?php
class Student {
private $grades;
private $average;
public function setGrades(array $grades) {
foreach ( $grades as $grade ) {
if (! filter_var ( $grade, FILTER_VALIDATE_INT ))
throw new Exception ( ‘A non-integer was found. Only integers are acceptable’ );
}
$this->grades = $grades;
}
public function CreateAverage() {
return $this->average = array_sum ( $this->grades ) / count ( $this->grades );
}
}
$jack = new Student ();
try {
$grades = array (
98,
95,
85,
79,
90,
92,
91
);
$jack->setGrades ( $grades );
// try this to see what type hinting does,
// $grades = 1;
// $jack->setGrades ( $grades );
} catch ( Exception $e ) {

} finally {
echo $jack->CreateAverage ();
}
// echo $jack->grades;[/php]

Actually, when it comes to OOP I don’t. I have also yet to “Need it”. All the talk of re-using code I can do with a function with a lot less code so that seems a meaningless point to me. At this point, I just cant see that oop is in any way better.

The latest example you show can be done with the same exact result with a lot less code in procedural and as a function is easily re-used.

I still spend time trying to grasp it though.

To me, this makes much more sense, is less code, re-usable and does the same exact thing. So… why do I need OOP?

[php]<?php
function student($grades)
{
if (is_array($grades))
{
foreach ($grades as $grade)
{
if (!filter_var($grade, FILTER_VALIDATE_INT))
throw new Exception(‘A non-integer was found. Only integers are acceptable’);
}
$average = array_sum($grades) / count($grades);
return $average;
}
else
{
return ‘Not Array.’;
}
}

$grades = array(98,95,85,79,90,92,91);
echo student($grades);
?>[/php]

I’ll interject my .02 cents.

Doing it the procedural way is fine if it is a small project, but let’s say it was for a school project that some local school system was developing. Using the class Student would be just one of many classes that the school system’s developer would be incorporating into the mix. An they could also throw in namespaces, for example maybe something like the following:
[php] <?php
namespace school_system\grading;

class Student {
private $grades;
private $average;
public function setGrades(array $grades) {
foreach ( $grades as $grade ) {
if (! filter_var ( $grade, FILTER_VALIDATE_INT ))
throw new Exception ( ‘A non-integer was found. Only integers are acceptable’ );
}
$this->grades = $grades;
}
public function CreateAverage() {
return $this->average = array_sum ( $this->grades ) / count ( $this->grades );
}
}[/php]

then the developer could develop without worry about variables (arguments) / functions (methods) colliding or using a different class(es) for various reasons. Or even choose the best class implementation among various developers without having any arguments that sometimes develops among developers. ;D

example:
[php]use school_system\grading\Student as Elementary;
$jack = new Elementary();[/php]

it’s a stupid set of examples, but I am just trying explain what I’m getting at (hopefully). ;D

I just learned about what you said about namespaces and naming collisions just the other day and in a group development that makes sense. Everything I do I am the only developer.

I will still keep trying to learn it. It’s something I should know, I may get handed a project that is written in oop and not be able to start from scratch.

Sponsor our Newsletter | Privacy Policy | Terms of Service