Solved

how does local variables know costtype=2 instantly

Posted on 2014-04-30
6
189 Views
Last Modified: 2014-04-30
<?php
abstract class Lesson {
    protected $duration;
    const     FIXED = 1;
    const     TIMED = 2;
    private   $costtype;

    function __construct( $duration, $costtype=1 ) {
        $this->duration = $duration;
        $this->costtype = $costtype;
    }

    function cost() {
        switch ( $this->costtype ) {
            CASE self::TIMED :
                return (5 * $this->duration);
                break;
            CASE self::FIXED :
                return 30;
                break;
            default:
                $this->costtype = self::FIXED;
                return 30;
        }
    }

    function chargeType() {
        switch ( $this->costtype ) {
            CASE self::TIMED :
                return "hourly rate";
                break;
            CASE self::FIXED :
                return "fixed rate";
                break;
            default:
                $this->costtype = self::FIXED;
                return "fixed rate";
        }
    }

    // more lesson methods...
}

class Lecture extends Lesson {
    // Lecture-specific implementations ...
}

class Seminar extends Lesson {
    // Seminar-specific implementations ...
}

$lecture = new Lecture( 51, Lesson::FIXED );
print "{$lecture->cost()} ({$lecture->chargeType()})\n"; 

$seminar= new Seminar( 4, Lesson::TIMED );
print "{$seminar->cost()} ({$seminar->chargeType()})\n"; 
?>

Open in new window


look at the call stack
line55
$seminar= new Seminar( 4, Lesson::TIMED );
enters constructor line8
    function __construct( $duration, $costtype=1 ) {
and the local variables know that $costtype is 2 before any if statement

I could understand
if parameter2:
costtype=2,
then
set costtype=2


how does local variables know costtype=2 instantly
0
Comment
Question by:rgb192
  • 2
  • 2
  • 2
6 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 40032270
Hi,

The local variables know because it was passed in as a parameter. In the Constructor

 function __construct( $duration, $costtype=1 )

$duration is a required parameter.
$costtype is an optional parameter,if you don't send a value for the parameter it will be set to 1.

So
$seminar= new Seminar( 4, Lesson::TIMED );
sets the second parameter, $costtype to 2

$seminar= new Seminar( 4, Lesson::FIXED);
sets the $costtype to 1 however, you could just call
$seminar= new Seminar( 4);
and get the same result...
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40032407
No points for this: it's only to clarify what jrm213jrm213 correctly said.

Lesson::TIMED sets the second parameter to 2 because TIMED is a constant you define at the beginning of your class setting its value to 2

abstract class Lesson {
    protected $duration;
    const     FIXED = 1;
    const     TIMED = 2;

Open in new window

0
 

Author Comment

by:rgb192
ID: 40032682
How are constants known outside class?
And how can seminar be called with
Only 1 parameter
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 250 total points
ID: 40032690
Yyou call them as parameter of a descendant class of Lesson, that is  Seminar: being Seminar a descendant of Lesson, it knows everything which is not private of the ancestor class.
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 250 total points
ID: 40032726
Hi,
How are constants known outside class?
The constants are within the class

abstract class Lesson {
    protected $duration;
    const     FIXED = 1;
    const     TIMED = 2;
    private   $costtype;

Open in new window



FIXED and TIMED are constant variables of the Lesson class.

And how can seminar be called with Only 1 parameter

When you create an instance of the class Seminar, it should run the parent constructor since it doesn't have one of it's own. Yhe constructor of the Lesson class takes 1 required parameter $duration and 1 optional parameter $costtype. So if you only supply 1 parameter when you create an instance of the Seminar class

$seminar= new Seminar(4);

the required parameter $duration will receive that value (4) and the optional parameter $costtype will be set to it's default value, which is 1.

see on php.net: http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default
0
 

Author Closing Comment

by:rgb192
ID: 40033618
thanks


FIXED and TIMED are constant variables of the Lesson class.
1 required parameter $duration and 1 optional parameter

 parameter of a descendant class of Lesson, that is  Seminar: being Seminar a descendant of Lesson, it knows everything which is not private of the ancestor class.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now