php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43720 Abstract method argument ignoring
Submitted: 2007-12-31 16:21 UTC Modified: 2008-01-01 16:18 UTC
From: phpbugs at dnr dot servegame dot org Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phpbugs at dnr dot servegame dot org
New email:
PHP Version: OS:

 

 [2007-12-31 16:21 UTC] phpbugs at dnr dot servegame dot org
Description:
------------
Related to: http://bugs.php.net/bug.php?id=36601

However, there's another case where it's nice if arguments for an abstract method can be ignored. I'd call it a 'loose abstract method' and maybe loose should be the keyword used to indicate it.

I bumped into this, when trying to make a simple container abstract class, that could be extended for specific containers (see example).

Not only is typehinting a problem, since inheritance isn't checked, but I'd like to be able to use more or less arguments depending on the container type.

So I guess, this is a feature request for a 'loose' abstract method declaration that simply ignores method arguments at compile time. The implemented method still needs to be checked for any type hints though.


Reproduce code:
---------------
<?php
abstract class Container
{
	protected $storage;
	public $index;
	
	public function __construct()
	{
		$this->storage = array();
		$this->index = 0;
	}
	
	/**
	 * @brief setter overloader
	 *
	 * Guards index from going negative, instead it goes to end of the array,
	 * minus the offset given.
	 */
	public function __set($nm, $val)
	{
		if( $val < 0 )
			$val = count($this->storage) - $val;
			
		if( $nm == 'index' )
			$this->idx = $val;
	}
	/* loose */ abstract public function pushBack($thing);
}

class Cookie
{
	protected $name;
	
	public function __construct($name)
	{
		$this->name = $name;
	}
}
class CookieJar extends Container
{
	public function pushBack(Cookie $cookie)
	{
		array_push($this->storage, $cookie);
		$this->index++;
	}
}

$jar = new CookieJar;

Expected result:
----------------
Nothing.

Actual result:
--------------
Compile time error.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-01 16:18 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

CookieJar hast to have a is-a relationship with Container and the methods have tot take the same parameters but Container::pushBack() would allow any type as parameter, but CookieJar::pushBack() just instances of cookie which violates the is-a relationship.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 13:01:28 2025 UTC