0

I'm unable to understand the function of __construct here. I know that in any class __construct runs anytime the class is instantiated. However, my curiosity is, when $marks is defined as null in the __construct function, then why it is checking for its value in the if statement of __construct? Won't it always return false?

class Marks_Manage
{
    public $mark_id;
    public $exam_id;
    public $class_id;
    public $subject_id;
    public $marks;
    public $attendance;
    public $student_id;
    public $marks_comment;
    public $created_date;
    //current_time( 'mysql' );

    public function __construct( $marks = null ) 
    {
        if($marks)
        {
            global $wpdb;
            $table_name = $wpdb->prefix . "marks";
            $mark_data = $wpdb->get_row( "SELECT * FROM $table_name WHERE mark_id=".$marks);
            $this->mark_id = $mark_data->mark_id;
            $this->exam_id = $mark_data->exam_id;
            $this->class_id = $mark_data->class_id;
            $this->subject_id = $mark_data->subject_id;
            $this->marks = $mark_data->marks;
            $this->attendance = $mark_data->attendance;
            $this->student_id = $mark_data->student_id;
            $this->marks_comment = $mark_data->marks_comment;

        }
    }
0

It's possible to pass variables to class constructs.

$blarg = new Marks_Manage( 'string' );

Now the conditional if( $marks ) returns true.

If nothing is passed to the class construct:

$blarg = new Marks_Manage();

Then $marks would be null and if( $marks ) evaluate as false;

  • But arent' we already setting the value of variable $marks to be null by defining it in class construct? – Himanshu Apr 27 '17 at 18:45
  • Only if nothing is passed to the class constructor. – Nathan Johnson Apr 27 '17 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.