Jump to content

Problem with abstract class


atitthaker

Recommended Posts

<?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.

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

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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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. 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.