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
https://forums.phpfreaks.com/topic/59909-class-to-class-function-call/
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;
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.