SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 47
  1. #1
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Private abstract method ?

    Why can't an abstract method be declared private ?

    as in this code :

    PageController.class.php
    Code:
    <?php
    
    abstract class PageController implements IHandler {
    
      protected $m_Locator;
      
      public function __construct( Request $request ) {
      
        $path = new PathInfoMapper( $this->map );
        $path->execute( $request );
      }
    
      public function doPreProcessing( ) { }
      public function doPostProcessing( ) { }
      public function execute( Locator $locator ) { }
    
      abstract function dodefault( );
      abstract function noaccess( );
    }
    Index.class.php
    Code:
    <?php
    
    class Index extends PageController {
    
      /* skipped snip */
      
      public function execute( Locator $locator ) {
      
        $this->m_Locator = $locator;
        
        $request = $locator->get( 'Request' );
        
        if ( $request->get( 'action' ) == null )
          $request->set( 'action', 'dodefault' );
          
        // add a layer of checking here on authorization access
        // Changes the event on access mapping
        // $amap = new UserAccessMap( $access_map, 'action', 'noaccess' );
        // $amap->execute( $locator );
    
        if ( method_exists( $this, $request->get( 'action' ) ) )
          eval( "\$this->".$request->get( 'action' )."( );" );
        else {
          echo "Invalid Action, launch the default action<br />";
          $this->dodefault( );
        }
      }
      
      public function dodefault( ) {
    
        echo "Default Action Index Page<br />";
      }
      
      public function noaccess( ) {
      
        echo "Person has no access to this action<br />";
      }
    
      private function add( ) {
    
        echo "Add Action Index<br />";
      }
    
      private function del( ) {
    
        echo "Delete Action Index<br />";
      }
    
      private function edit( ) {
    
        echo "Edit Action Index<br />";
      }
    IHandler.class.php
    Code:
    <?php
    
    interface IHandler {
    
      public function doPreProcessing( );
      public function doPostProcessing( );
      public function execute( Locator $locator );
    
    }
    
    ?>
    The reason I want them private is that I want to restrict their call from the execute( ) function only like the other defined "action" functions which are private and work. I declared them abstract because I want to force them to be defined in the inherited class.

    And if people are interested, I can post my whole little framework code that's based on arborint's skeleton framework, recoded in PHP5 with modifications to suit my needs for access control on both the page (before the FrontController) and action (in the PageController) level.

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply because PHP has followed Java's model, where the access keywords are used for two purposes: the at the same time define access (who can access which property directly) and extendability (which properties can be used from a subclass without overriding it). Specifically, a public property is accessable by everyone; a private property can't be used by no other class except the one in which it is defined (although it can be overriden by subclasses); and protected property is accessible only by the class and its subclasses. So you need protected, not private.

    IMO, those three "levels" (Java has a fourth, "package"; will PHP6 introduce a "namespace" one?) are silly; we only need two, public and private, which are equivalent of the current public and protected respectively (IIRC this is how it0s handled in Smalltalk).

  3. #3
    SitePoint Addict mx2k's Avatar
    Join Date
    Jan 2005
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    namespaces are more for solving naming conflicts by branching out classes with the same name, hiding classes with the same name rather than hiding visibility of functionality as public, protected and private do.

  4. #4
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know, I was referring to the tendency to follow Java pretty closely.

  5. #5
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ClickHeRe
    Code:
      public function __construct( Request $request ) {
      
        $path = new PathInfoMapper( $this->map );
        $path->execute( $request );
      }
    I would think twice about triggering such an action in the constructor. Constructors are generally meant to be safe. Furthermore, you seem to be mixing passing arguments vs. passing a Locator, with the same object nonetheless. Could be I just don't get your solution, but sounds like an odd one.

  6. #6
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    ... IMO, those three "levels" (Java has a fourth, "package"; will PHP6 introduce a "namespace" one?) are silly; we only need two, public and private, which are equivalent of the current public and protected respectively (IIRC this is how it0s handled in Smalltalk).
    Don't understand exactly, which two would be sufficient in your opinion? I can't relate to that, because I see use for all three visibility levels, and not just for exotic cases.

    Regards

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    Norway
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    Don't understand exactly, which two would be sufficient in your opinion? I can't relate to that, because I see use for all three visibility levels, and not just for exotic cases.

    Regards
    I think he's saying we only need to access levels; one public, the same as the public in PHP, and one private, which essenially be renaming the protected access mode in PHP. Am I right?

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I understand he thinks "we" only need two levels, but I just wasn't sure about which two

    The thing is, I belive both -- private and protected -- have their use, and so does public. So, imo we need all three

    Regards

  9. #9
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you can find use for everything.

    The limited extendability of a property IMO defies the concept of subtyping. Seveda.

  10. #10
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    Well, you can find use for everything.
    Yeah if I had a zwibble I could probably find a use for it, but that doesn't make it useful or necessary.

    On the other hand, if you don't want all 3, you can stick to public/protected. I've seen quite a few devs doing that.

  11. #11
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    On the other hand, if you don't want all 3, you can stick to public/protected. I've seen quite a few devs doing that.
    I'm one of them.

  12. #12
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    I'm one of them.
    I lean that way myself. I never intend to create deep hierarchies of classes, but I also fail to see a need to criple that hierarchy with private methods or attributes. Similar for "final" classes.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  13. #13
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    Norway
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    Yes, I understand he thinks "we" only need two levels, but I just wasn't sure about which two

    The thing is, I belive both -- private and protected -- have their use, and so does public. So, imo we need all three

    Regards
    "We" as in people using PHP.

    I never use private access myself, becuase I have to this day failed to see any real need to hide anything from extending classes.

  14. #14
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, works on with protected.

    Ezku, yes I have moved it now as I've figured out another way to do the same thing.

  15. #15
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, what happens when we get PHP6 and hopefully namespaces? Protected in Java lets all classes defined in the same package read each others protected members. PHP6 might decide to do it the same. Will this be a problem?

  16. #16
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ryan Wray
    Protected in Java lets all classes defined in the same package read each others protected members.
    No, that's the "package" level. Java has PPPP, not PPP.

  17. #17
    SitePoint Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Java Visibility

    Level: Visible to self and ...
    private: none
    default: package
    protected: package, children
    public: all

    This is something which allways struck me as strange about javal. Classes in the same package seem to get a higher priority (in terms of what's visible to them) than subclasses.

  18. #18
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    No, that's the "package" level. Java has PPPP, not PPP.
    No, actually, protected allows subclasses and classes in the same package to access the members.

    Use this as reference: http://java.sun.com/docs/books/tutor...sscontrol.html

    So, my questions remains, if PHP6 takes after Java and has namespaces, will the protected keyworld have any nasty implications. I suppose not, if all developers discipline themselves. Hopefully when developing PHP6 realises alot of developers use protected instead of private, and don't follow Java way.

  19. #19
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As someone mentioned, packages and namespaces are not the same thing. You could actually have a same class in different namespaces...

  20. #20
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you not want to follow in the footsteps of Java? I don't mean the whole hog, but maybe just cutting the fat away from it, ie Take the best of it and implement it for the object model for PHP6.

    There is nothing at fault in any way, shape or form with Java as it stands at the moment, hell it even beats down Ruby on Rails, not that that wasn't difficult to do in the first place anyways

    PHP could mature in a lot of ways simply by learning from Java, despite the two languages having a completely differnet environment.

    You shouldn't knock Java. There is a lot of scope to learn from it.

  21. #21
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Defining private methods in an interface doesn't make any sense because an interface is a PUBLIC contract with outside resources, and an interface is essentially entirely abstract.

    REQUIRING something to implement a private method is dictating implementation -- which defeats the entire purpose of abstract classes and interfaces in the first place.

    I never intend to create deep hierarchies of classes, but I also fail to see a need to criple that hierarchy with private methods or attributes.
    Simply put, private variables are necessary to prevent other programmers (and yourself) from making stupid mistakes. With private data, you only have to validate the input when it comes in through __set (or set<Var> or however you have it implemented). When developers can arbitrarily overwrite that value at any time, it means that you constantly have to validate it before you can do anything with it. Then again, most people do a piss-poor job of validating their data anyway, which is why most code out there is full of security holes and bugs.

    public, private, and protected exist in virtually all OO languages for a reason -- because all 3 are useful. Sure, you won't be using protected as much as you'd use private or public. Half a century (or more?) of OO coding has taught us that having different levels of access to data is a good feature to have. There's nothing wrong with that.

  22. #22
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    -- but maybe just cutting the fat away from [Java], ie Take the best of it and implement it for the object model for PHP6.
    This might be a matter of preference, but I don't find anything splendid in Java's object model. If you want a similar example on how some things should be done, look at C#, which is kinda Java++: you can find transparent accessors, delegates, operator overloading - all sorts of features that make a language more enjoyable. (Am I getting repetitious here?)

  23. #23
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Why would you not want to follow in the footsteps of Java?
    a) Java is statically typed language, PHP is a dynamic one.
    b) Java is a general purpose language, PHP is Web development language.
    c) Java is strictly object-syntax oriented, PHP mixes and matches object and procedural.
    d) Java has in-memory objects, PHP has single-pass execution.
    e) Java is compiled (if only to bytecode), PHP is interpreted.

    There are probably more differences, but these are important enough. Take e) for example -- this is the sole reason why XML configuration files are better suited for Java than for PHP, whish has include(), variable variables and similar feats not available to compiled languages.

    Note: I'm not arguing which options above are better -- just pointing the differences.

  24. #24
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    As someone mentioned, packages and namespaces are not the same thing.
    Whatever you call them, modules, namespaces, packages quite often they have similar approaches. And while the planned implementation of namespaces for PHP6 is quite different than Java packages, they have similaraties. One is multiple classes can be defined in a namespace.

    This brings on the issue: do we allow classes defined in the same namespace have access to each other members under any condition other than public? If we follow Java, this is what protected will allow us. If we decide to add another level, we will probably have to add a new keyword (Java has the package level implicitly if no other level is assigned, in PHP public is default, changing this might break backwards compability in alot of cases). The other option is not to allow classes defined in the namespace to view non-public members, though does this appear strict?

    I don't know if I am making sense to anyone, but oh well.

  25. #25
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, no OO development needs any levels besides public and private (acting as current protected). Everything else just makes the point of access control unclear, which helps no one. So, since we already have a keword too many, I am against any further levels, namespace or any other.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •