1

My question is similar to Call function in function from another class PHP, but its answer does not work for me.

Basically here is what I want to do.

in 'a.php', I defined class A.

class A{ function ab(){} function cd(){} function ef(){} ... } 

in 'b.php', I would like to define class B that can call the function in class A. Here is what I put, but it seems not work.

class B{ function xy(){ require 'a.php'; $a = new A(); $this->x = $a->ab(); $this->y = $a->cd(); return $a->ef(); } } 

Can anyone point me to the right direction? Thanks

========

UPDATE: to make my question more clear I made another example.

class Person{ function firstName(){return $this->firstName;} function lastName() {return $this->lastName;} function address() {return $this->address;} ... } class Car{ function ownerInfo(){ $owner = array(); require 'person.php'; $p = new Person($this->ownerID); $owner['firstname'] = $p->firstName(); $owner['lastname'] = $p->lastName(); $owner['address'] = $p->address(); //I am sure the data is there, but it returns nothing here return $owner; } } 
10
  • 4
    Inheritance?....Just extend class B to class A and you can use those methods defined in class A Commented Jan 29, 2013 at 6:43
  • extend the class B with class A Commented Jan 29, 2013 at 6:43
  • not sure for php but i know that in c++ you include (require) the file in the header (well the beginning of the file, or before the class B deceleration) Commented Jan 29, 2013 at 6:44
  • 1
    actually A and B are different model so I don't want to use "class B extends A". Most the other functions in A and B are not related. Commented Jan 29, 2013 at 6:49
  • I tried to put the require statement in the beginning of the file but it does not work either. Commented Jan 29, 2013 at 6:49

1 Answer 1

1

Try like this

class B extends A{ function xy(){ require 'a.php'; $a = new A(); $this->x = $a->ab(); $this->y = $a->cd(); return $a->ef(); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Ofcourse its not good but we can use like this right because it may extract a.php file whenever this class B is used,orelse it may decrese to load a.php which unvaluable..??
is there any other way to do that? actually I already put "class A extends C" and "class B extends D"
Adding the extends A does nothing. It only adds to code confusion.
@coolmoon you can do like this and there is NP if you extend others to others

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.