HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

History homework class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
homeworkclasshistory

Problem

I'm yrying to practice some OO PHP and I'm just wondering if what I'm doing is okay or not. Please tell me if there are any alarms in my method so that I can stop doing it and learn a better way.

In my conditional statement, I was following the rule of no else keyword, based on this article by William Durand, which talks about Object Calisthenics.

I also don't know if I'm using the abstract properly, being that I learned it just a few minutes ago - but it works, so I suppose it's being used correctly.

coursesTaken = $coursesTaken;
            }

            public function completedOrNot()
            {
                if ($this->coursesTaken >= $this->minimumCourses)
                {
                    return "You completed the course in " . $this->coursesTaken
                    . " classes, but you only needed " . $this->minimumCourses
                    . " classes to complete the course! \n\n";
                }

                return "Sorry, you did not complete this course. You only took "
                . $this->coursesTaken . " classes, and you need a minimum of "
                . $this->minimumCourses . " to pass. \n\n";
            }
        }

        $student1 = new HistoryHomework(11);
        echo $student1->completedOrNot(); // You have completed the course in 11 classes, but you only needed 10 classes to complete the course!

        $student2 = new HistoryHomework(7);
        echo $student2->completedOrNot(); // Sorry, you did not complete this course. You only took 7 classes, and you need a minimum of 10 to pass.

Solution

It's opinion-driven, but general points:

  • You return large strings. Use try/catch for custom errors and warnings.



  • Manage specific messages separately from your OOP (for example, language files).



  • Separate layers, such as presentation, application, business logic, etc.



About the further structure: correct, but you didn't post serious logic.

There are many patterns to solve problems, such as Model-View-Controller (separating responsibilities), Adapter pattern, Factory pattern, etc. Read more here.

Context

StackExchange Code Review Q#100358, answer score: 2

Revisions (0)

No revisions yet.