Solved

variables with 2 arrows

Posted on 2014-04-30
10
154 Views
Last Modified: 2014-05-01
<?php
abstract class Lesson {
    private   $duration;
    private   $costStrategy;

    function __construct( $duration, CostStrategy $strategy ) {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }

    function cost() {
        return $this->costStrategy->cost( $this );
    }

    function chargeType() {
        return $this->costStrategy->chargeType( );
    }

    function getDuration() {
        return $this->duration;
    }

    // more lesson methods...
}


abstract class CostStrategy {
    abstract function cost( Lesson $lesson );
    abstract function chargeType();
}

class TimedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return ( $lesson->getDuration() * 5 );
    }
    function chargeType() {
        return "hourly rate";
    }
}

class FixedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return 30;
    }

    function chargeType() {
        return "fixed rate";
    }
}

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

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

$lessons[] = new Seminar( 4, new TimedCostStrategy() );
$lessons[] = new Lecture( 4, new FixedCostStrategy() );

foreach ( $lessons as $lesson ) {
    print "lesson charge {$lesson->cost()}. ";
    print "Charge type: {$lesson->chargeType()}\n";
    print "end lesson<br>";
}
?>

Open in new window



    function cost() {
        return $this->costStrategy->cost( $this );
    }

    function chargeType() {
        return $this->costStrategy->chargeType( );


I do not understand variables with
2
->
how to create smaller variables and do var_dump
0
Comment
Question by:rgb192
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 40032305
The multiple arrows in something like this are indicators of an inheritance cascade.

return $this->costStrategy->chargeType( );

To find out what's going on, look at the class definition for costStrategy, then follow that to the method definition for chargeType.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 40032349
1)
return $this->costStrategy->chargeType( );

$this = object of type Lesson
costStrategy = object of type TimedCostStrategy

In other words, multiple-arrows-properties mean that a property is not a string or an integer but an object whitch has itself its properties.

In your case, chargeType is a method which returns a string. This method belongs to the class TimedCostStrategy which is a property of the class Lesson

2)
It doesn't make sense to build smaller variable: one of the advantages and main charateristics of oop is the inheritance, tha ability to inherit from another object method and properties building a structured system. If you built a different class in order to have smaller variables, you shoud reduce the oop implementation, making all method and properties of the CostStrategy abstract class (and of its descendants) be methods and properties of the class Lesson itself, but this would be a regression in the way you're doing forward OOP programming style (and you have yet done a lot of miles in that way to capome back now, I think).
0
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 200 total points
ID: 40032360
The arrow (->) just means that the variable to the left is the object and the variable to the right is a property or method of the object

So if you have

$a->b->c;

What this is saying is that

$a is an instance of an Object that has a property $b which in turn has a property $c.

In practical terms
class C {
   public $c = 5;
}

class A {
  public $b;
  function __construct()
  {
    $b = new C();
  }
}

$a = new A();

echo $a->b->c; // output 5.

Open in new window


This is the same as
$x = $a->b;
$y = $x->c;

Open in new window

In the example you have given.
$this->costStrategy->cost( $this )

Open in new window


This is the same as
$cs = $this->costStrategy; // Get the costStrategy property from the current instance
$cs->cost($this); // Call the method on the costStrategy object

Open in new window

In the above example the costStrategy property is itself an object and so we can perform what is called chaining which is to have one object contain a property which is an object which can contain a property etc. Instead of writing the assignments out one at a time
$x = $a->b;
$y = $x->c;

Open in new window

We can chain them together
$x=$a->b->c;

Open in new window

0
 

Author Comment

by:rgb192
ID: 40033604
return $this->costStrategy->chargeType( );

$this = object of type Lesson
costStrategy = object of type TimedCostStrategy


which chargeType, which timedcoststrategy
because both methods are in abstract class


It doesn't make sense to build smaller variable: one of the advantages and main charateristics of oop is the inheritance, tha ability to inherit from another object method and properties building a structured system. If you built a different class

smaller variables
$obj->method(new obj2())
instead use
$obj2=new obj2();
$obj->method($obj2);


more comments soon...
0
 

Author Comment

by:rgb192
ID: 40033608
<?php
class C {
   public $c = 5;
}

class A {
  public $b;
  function __construct()
  {
    $b = new C();
  }
}

$a = new A();

echo $a->b->c; // output 5.

Open in new window


no output

Notice: Trying to get property of non-object in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname311.php on line 16



/*this is lesson*/$this->costStrategy->cost( /*what is the second this*/$this )
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.

 

Author Comment

by:rgb192
ID: 40033614
To find out what's going on, look at the class definition for costStrategy, then follow that to the method definition for chargeType.

abstract class CostStrategy {
    abstract function cost( Lesson $lesson );
    abstract function chargeType();
}


    function chargeType() {
        return "hourly rate";
    }

    function chargeType() {
        return "fixed rate";
    }


i still do not understand when looking at the class definition and methods together
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40033687
Sorry, I don't know what else to say.  This is notation -- a formal way of expressing an idea.  Can you tell us in plain language what your idea is?
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 200 total points
ID: 40033891
1) Look at your code:

<?php
class C {
   public $c = 5;
}

class A {
  public $b;
  function __construct()
  {
    $b = new C();
  }
}

$a = new A();

echo $a->b->c; // output 5.

Open in new window

It generates Notice: Trying to get property of non-object in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname311.php on line 16 because... it's wrong! Simply change this

  function __construct()
  {
    $b = new C();
  }

Open in new window


in this

  function __construct()
  {
    $this->b = new C();
  }

Open in new window


and it will output 5 as expected. Using $this is not an option.

2)

return $this->costStrategy->chargeType( );

$this = object of type Lesson
costStrategy = object of type TimedCostStrategy


which chargeType, which timedcoststrategy
because both methods are in abstract class
We're beginning to make some confusion here: timedcoststrategy is not a function, it's a class, a class which extends an abstract class, CostStrategy.

 - The class TimedCostStrategy makes concrete the class CostStrategy which is abstract
 - the class TimedCostStrategy implements the method chargeType() wich is abstact in   CostStrategy class

The fact a method is declared in an abstract class doesn't make it "non-existent": you just can't use directly the abstract class in a actual code and you must implemet a descendant class instead, but an abstract class can declare properties and methods of other abstract classes.

You can think to absract classes and to their relationship as to the technical design of an archtect or an engineer: these design a house, but you can't live in it. You must make it be built following the design but using real bricks, and wood and maybe iron.
Abstract classes tell you how you have to build your application (or a specific part of your application) nad you'll have to follow that design to build object which will make your app working fine.

I really can't say it better than this.

3)
abstract class CostStrategy {
    abstract function cost( Lesson $lesson );
    abstract function chargeType();
}


    function chargeType() {
        return "hourly rate";
    }

    function chargeType() {
        return "fixed rate";
    }


i still do not understand when looking at the class definition and methods together

The abstract class CostStrategy states each its descendant must implement at least two methods: cost() and chargeType().
The two descendant classes TimedCostStrategy and FixedCostStrategy does this giving to these methods two different implementation. So, when you'll open your personal "OOP programming in Php High School" you'll be able to build an application to manage your revenues accordingly to the lesson type your stundens will subscribe: someone will choose a fixed cost and in this case we can imagine the lesson will have a fixed duration after which the student, if he'll don't have understood something will have to buy another lesson; someone will choose the flexible lessons: we can imagin these lessons will can go on for a long time and their cost will depend on their real duration.
So with your app you'll can use TimedCostStrategy or FixedCostStrategy  to emit invoices for your students accordingly to their choices when they subscribed your course.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 40033964
@marco - thanks for catching the missing $this - I unintentionally ommitted it in my post.

@rgb192 Do you understand the concept of overriding functionality?

If you have a class B that extends Class A. Any method in B that is the same name as a method in A overrides the method in A.
class A
{
    function test()
    {
        echo 'Class A test<br/>';
    }
    function different_test()
    {
        echo 'Class A different_test<br/>';
    }
}
class B extends A
{
    function test()
    {
        echo 'Class B test<br/>';
    }
}
$b = new B();
$b->test(); // Class B test
$b->different_test(); // Class A different test
$a = new A();
$a->test(); // Class A test
$a->different_test(); // Class A different test

Open in new window

Class B extends A so the test() function in B supercedes that in A.
However the different_test function exists only in A so when invoked in either $a or $b the Class A function runs.
When finding a method to run the interpreter will look in the instantiated class first - if not found it will look in the parent - if not found its parent etc until it finds the first class in the hierarchy with a matching method - which it then executes.
0
 

Author Closing Comment

by:rgb192
ID: 40035855
The arrow (->) just means that the variable to the left is the object and the variable to the right is a property or method of the object


so class A variable
$this->b = new C();

The fact a method is declared in an abstract class doesn't make it "non-existent": you just can't use directly the abstract class in a actual code and you must implemet a descendant class instead, but an abstract class can declare properties and methods of other abstract classes.

The two descendant classes TimedCostStrategy and FixedCostStrategy does this giving to these methods two different implementation. So, when you'll open your personal "OOP programming in Php High School" you'll be able to build an application to manage your revenues accordingly to the lesson type your stundens will subscribe


While, I did not find information about inheritance cascade, it is good to put a word to the line of code

thanks
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

Title # Comments Views Activity
can windows 10 wamp send mail()? 3 32
Should I use subdomains or addon domains? 3 33
Whether to store ID's or text values for detailed information 3 20
Session timeout 5 13
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 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

23 Experts available now in Live!

Get 1:1 Help Now