Jump to content

class to class function call


Recommended Posts

Okay, I have two classes set up. Both classes have functions. For example, we will call them class1 and class2.

 

so i have class1 and class2 called like so:

 

$class1 = new class1;

$class2 = new class2;

 

now then, can class2 reach into class one and grab a function? and if so, how do I accomplish that?  do i have to do:

 

class class2

{

function tester() {

class1func();

}

 

or do i have to do:

 

class class2

{

function tester() {

global $class1

$class1->class1func();

}

 

???

 

the reason i ask is i created a mysql class that has my most used mysql functions simplified for me, and I want to use my mysql query function from the mysql class in my news class to draw the news from the database.

Link to comment
Share on other sites

2 ways.

 

1. Object A uses instance of Object B

<?php
class a {
  function a () {
    $this->b = new b;
  }

  function get() {
    return $this->b->val;
  }
}
?>

 

2. Object B extends Object A

<?php

class a {
   public $val = "hio";
   function a() {
     $this->val = "hello";
   }
}

class b extends a {
  function b() {
    a::a();
    print $this->val;
  }
}

Link to comment
Share on other sites

That second example should be...

 

<?php

class a {
   public $val = "hio";
   function a() {
     $this->val = "hello";
   }
}

class b extends a {
  function b() {
    $this->a();
    print $this->val;
  }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.