Jump to content

class problem


lenerd3000

Recommended Posts

hello,

 

i have 2 classes on different page and will inherit a class from a 3rd page.

 

now, i have my index page with a class inherited the class on the 3rd page.

 

when i try to get function from the parent its ok but my problem is

 

when i try to access the functions from the first 2 classes...

 

how can i call the class that is a member of my parent??

 

test1.php

 

class test1 extends test

{

      function t1()

      {

            echo "test1";

      }

}

 

test2.php

 

class test1 extends test

{

      function t2()

      {

            echo "test2";

      }

}

 

test.php

 

class test

{

      function t()

      {

            echo "test";

      }

}

 

index.php

 

class access extends test

{

}

 

$test = new test();

 

$test->t; // OK

 

$test->t1; // ERROR

 

$test->t2; // ERROR

Link to comment
Share on other sites

I would make it so the classes keep building on eachother...

 

<?php
class test
{
      function t()
      {
            echo "test";
      }
}
?>

 

<?php
class test1 extends test
{
      function t1()
      {
            echo "test1";
      }
}
?>

 

<?php
class test2 extends test1
{
      function t2()
      {
            echo "test2";
      }
}
?>

 

<?php
class access extends test2
{

}

$test = new access();
$test->t();
$test->t1();
$test->t2();
?>

Link to comment
Share on other sites

I guess I don't understand what you are trying to do. Are you trying to declare a class, then programatically add methods to it? That is not possible.

 

Can you try to explain what you are looking to specifically accomplish instead of speaking generically?

Link to comment
Share on other sites

its too simple logic but hard for me to do.

 

i want to use a class that calls all the classes i write base on what i need in one call.

 

i have test1, test2, test3 classes separately and i will make a test4 class

 

which if call, test1-test3 class will be inherited.

 

do you know cakephp? i want to make framework and i base in on cake

 

i dont know how did they integrate all the class and function in cake but when i

 

try to trace it up, all of them inherits a class named Object.

 

and all of the built-in classes can be call using $this->

 

but i cant figure out where did they made a connection for all the class.

 

 

Link to comment
Share on other sites

you must include the class on all pages that have inheritance.  While php will allow you to pass the values of the object, the functions are not passed and you have to include them.

 

ie:

 

require_once("test.php");

class test1 extends test {
  function t1(){
    echo "OK";
  }
}

class test2 extends test1 {
  function t2() {
     echo "OK"; 
  }
}

$new_object = @new test2;
$new_object->t();             // OK
$new_object->t1();           // OK
$new_object->t2();           // OK

 

Notice that the call to the class is the last extended class in the structure.  If you call the first class, there's no way to access the other functions in the other extended classes.

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.