php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32155 Add Object cast support
Submitted: 2005-03-01 20:17 UTC Modified: 2013-10-29 07:11 UTC
Votes:6
Avg. Score:4.3 ± 0.5
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:3 (75.0%)
From: anferr at mecon dot gov dot ar Assigned: krakjoe (profile)
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anferr at mecon dot gov dot ar
New email:
PHP Version: OS:

 

 [2005-03-01 20:17 UTC] anferr at mecon dot gov dot ar
Description:
------------
It'll be very useful to add the ability to cast an object to a parent class.
A simple example could be security: having a base class with attributes, is extended by another that can do CRUD operations on DB. When passing to VIEW layer (according to MVC), just do a cast to base object, and CRUD actions will be disabled.

Reproduce code:
---------------
class Person {
  protected $age;
  function getAge() { return $this->age; }
  function setAge($age) {$this->age = $age; }
}

class PersonDB extends person {
  function load($id) {
    // SQL execution
    $this->setAge($resultSet->age);
  }
}

in Model layer:
$objPersonDB = new PersonDB();
$objPersonDB->load($id);

in Controller layer, sends $person to View layer:
$person = (Person)$objPersonDB;

Expected result:
----------------
if I call, in View layer:
$person->load(5);
must raise an error

Actual result:
--------------
cast do nothing, method is invoked.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-29 07:11 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: krakjoe
 [2013-10-29 07:11 UTC] krakjoe@php.net
This doesn't make good sense.

PHP is single inheritance; to be able to meaningfully cast an object you must have multiple inheritance, and you must implement properly all the methods required.

What you want to do is not cast at all, it is inherit.

Closing the request.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 21:01:35 2025 UTC