Jump to content


Photo

Problem with abstract class


  • Please log in to reply
3 replies to this topic

#1 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 16 August 2006 - 04:34 AM

<?php

abstract class Manager {
    abstract protected function work();
    abstract protected function WorkArea();
    protected function showSalary($basic)
    {
        print("Salary is " . $basic*2.5);
    }
}

class ProductionManager extends Manager {
    protected function work()
    {
        print("</br>Manages production of the units...</br>");
    }
    protected function WorkArea()
    {
        print("</br>Works in factory mainly and manages production of the units...</br>");
    }
}

class MarketingManager extends Manager{
    protected function work()
    {
        print("</br> Manages marketing work");
    }
    protected function workArea()
    {
        print("Works in field and guide other sales executive");
    }
}

$mm=new MarketingManager ();
$mm->work();
$mm->workArea();
$mm->showSalary(8000);

$pm=new ProductionManager;
$pm->work();
$pm->workArea();
$pm->showSalary(10000);
?>

Above I have defined three classes but the methods work() and workarea() are not accessible until I make them public.
When protected it is giving following error.

Fatal error: Call to protected method MarketingManager::work() from context '' in /var/www/html/trainingweb/modules/test/TestAtitProblem.php on line 35

As far as I know when class is abstracted it's visibility can be same or weaker(in this case it is same), then what's the problem?
Plz help...
Beginner with PHP and need help

#2 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 16 August 2006 - 05:39 AM

By declaring your method definition as protected it is not usable outside the class(or it's children). If you want to implement the method in code that would need access then it needs to be declared public.

Amendem: The abstract protected is fine in the Manager class.
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#3 atitthaker

atitthaker
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationBanglore, India

Posted 16 August 2006 - 06:22 AM

K.I tested as u told me.
U can't access protected method or variable outside from the class, so I need to make another method to call protected method and this method would be public. This works finely.

regards
Atit
Beginner with PHP and need help

#4 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 16 August 2006 - 06:32 AM

Well sure that would work, but if the purpose of the method is to be called from an instance of the object why wouldn't you just want to make it public anyway? The whole purpose of making a function or variable protected or private is because you don't want them to be directly accessed. If the logic of your class is that you(or anyone using your class) should be able to call $obj->work() to print out the line then by design the method should be public.

Just sounds like your creating a second public class to call a protected one for no reason. It's not necessary to protect a method just because you can. 
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users