Jump to content

question on OOP


superaktieboy

Recommended Posts

Hi,

 

I am looking into a way of adding addons to a class I made.. I thought something like this would work, but not sure how to implement it:

 

<?php
class foo {
    function foobar($text) {
        $text = $text . 'b';
        return $text;
    }
}
class bar extends foo {
    function foobar($text) {
        $text = $text . 'c';
        return $text;
    }
}

$var = new bar();
$var->foobar('a'); // this would then return abc
?>

 

Now i want to be able to call up foo, and it calls the foobar of foo, and the foobar of bar.

is that possible?

 

hosh

Link to comment
Share on other sites

Now i want to be able to call up foo, and it calls the foobar of foo, and the foobar of bar.

is that possible?

No. Extensions only go one way. bar can see foo's methods, not the other way around.

What are you trying to accomplish?

 

You could have one parent class, and two subclasses, then both subclasses can have the parent's methods. But they can't see each other's content at all.

Link to comment
Share on other sites

what i was looking to do is extend on a class (multiple times), call one method on the parent class, and the parent class calls a method in the subclasses, as they edit a specific text.. e.g.

file main.class.php

static class main{
    public static function addToText($text);
}

 

file plugins/something.class.php

class something extends main() {
    function addToText($text) {
         return $text.'a';
    }
}

 

 

file plugins/somethingElse.class.php

class somethingElse extends main() {
    function addToText($text) {
         return $text.'b';
    }
}

 

this way i would include everything in plugins folder, and it would automatically be initiated and when calling main::addToText(), it would call the rest of the classes' addToText() function.

Link to comment
Share on other sites

I could be completely wrong, but wouldn't this work:

 

class bar extends foo {
    function foobar($text) {
        parent::foobar($text);
        $text = $text . 'c';
        return $text;
    }
}

 

So you call the parent-function inside the extended function...

Link to comment
Share on other sites

It sounds to me like there are two concepts you should explore:  abstract classes and interfaces.  An interface is basically a list of method signatures that must be implemented by any class that is declared to "implement" that interface.  In your example, rather than having all the plugins extend a class, it might make sense for them simply to implement an interface of methods that they need to to support.  Since you know that they will not work unless they implement all the required functions in the interface, you can know in advance that you can manufacture and work with any of those objects via their public interface.  With that said, because you can not define in advance a return parameter, there is plenty of ways that things could go really wrong, but then that is the nature of a loosely typed language like php.

 

The other possibility is to start with an abstract class.  Abstract classes are similar to interfaces in that they have a basic signature of methods.  What is different from an interface, is that the abstract class can have non abstract methods in it.  You can not instantiate an object of an abstract class with new, so it's really meant to be a common ancestor.  You declare any of the methods that you intend to have overridden in the plugin classes to be abstract, and then those abstract classes are expected to implement them, so again you can write code that calls those abstract methods, knowing that they will be available.

 

 

 

 

Link to comment
Share on other sites

thanks for the replies guys, this did the trick for me:

<?php

class lol {
    private $lol = 'HA';
    private $extensions = array();
    function add(ext $obj) {
        $this->extensions[] = $obj;
    }
    
    function loop($text) {
        foreach($this->extensions as $obj) {
            $text = $obj->parse($text);
        }
        return $text;
    }
}

abstract class ext {
    public abstract function parse($text);
}

class ext1 extends ext {
    public function parse($text) {
        return $text . 'ext1';
    }
}
class ext2 extends ext {
    public function parse($text) {
        return $text . 'ext2';
    }
}


$lol = new lol();
$lol->add(new ext1());
$lol->add(new ext2());
echo $lol->loop('test');
?>

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.