1

Inheritance and except some attribute in OOP php?

I HAVE CLASS A

class A{
    var a;
    var b;
    var c;
    var d;

    function todo1()
      {
            //more code here 
      }
     function todo2()
      {
             //more code here 
      }
     function todo_with_var_c()
      {
             //more code here 
      }

    }

$A_ = new A;

I want get a new object $B the same object of A but except var c todo_with_var_c

So I tried with inheritance in php

class B extends A{

     //TODO
}

$B_ = new $B;//$B_ will the same object  $A_ but except `var c`
and and `todo_with_var_c`

How Can I do it? thanks!

tree em
  • 20,379
  • 30
  • 92
  • 130
  • Why do you need this, anyway? You should be able to go around the fields and methods you don't need simply by not using them. Or am I missing something about your problem? – Czechnology Mar 01 '11 at 11:36

3 Answers3

7

Think about the word extends for a second. It implies the child class is an extension of the parent. Think: more than or special case or "B is an A with added responsibility". This means, you can add stuff, but you should not remove anything. If B is supposed to have less properties, B should be the parent and A the extension, e.g. the other way round.

If you want to hide properties and/or methods in a subclass, use visibility.

class A
{
    protected $a;
    protected $b;
    private $c;
    protected $d;

    public function todo1() { … }
    public function todo2() { … }
    private function todo_with_var_c() { … }
}

Note that I made the properties protected, while in your code they are public because var is old PHP4 syntax (which you should no longer use) when there was no visibility. Properties should not be public though, but accessed through accessors methods (though opinions on that vary).

Further learning resources:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • we should not change the class A ,because class A used in other code.is is possible if we dont want to change the behaviour of A – tree em Mar 01 '11 at 11:31
  • 2
    Actually this is better than the unset-method one. – Shoe Mar 01 '11 at 11:32
  • think you should explain your answer a bit to help him understand – Imran Omar Bukhsh Mar 01 '11 at 11:32
  • @IEnAk, you are not changing the behavior of the class A, you are just making some things of class A not available in class B, with scope keyword 'private' and 'protected'. – Shoe Mar 01 '11 at 11:34
  • correct approach, but you should add the `$` to the instance variables, http://www.php.net/manual/en/language.oop5.visibility.php, this throws an error as is, `syntax error, unexpected T_STRING, expecting T_VARIABLE` – xzyfer Mar 01 '11 at 11:35
  • 1
    The only possible issue would be if he was already breaking encapsulation by accessing `$c` directly. Which lets face it, is more than likely. – xzyfer Mar 01 '11 at 11:37
  • 1
    @Gordon thanks for your answer and good resources.and thanks everybody that leaved the comments.Help better . – tree em Mar 01 '11 at 11:47
0

use access modifer for accessing variable or function

class A{
    protected $a;
    protected $b;
    private $c;
    protected $d;

    public function todo1()
      {
            //more code here 
      }
     public function todo2()
      {
             //more code here 
      }
     private function todo_with_var_c()
      {
             //more code here 
      }

    }
Jeg Bagus
  • 4,895
  • 9
  • 43
  • 54
0

You can set the visibility of the variables so that only those you want are visible in the inherited class. In your example, setting the var c to private would only make it available to the class A; e.g..

class A {
  public $a;
  public $b;
  private $c;
  public $d;

  ...

}

The only possible drawback is that to access $c outside of class A you would need a getter method. You can get more info on visibility (and more detailed examples) here: http://php.net/manual/en/language.oop5.visibility.php

John C
  • 8,223
  • 2
  • 36
  • 47