Asked  7 Months ago    Answers:  5   Viewed   30 times

I need to determine, after all files have been included, which classes extend a parent class, so:

class foo{
}
class boo extends foo{
}
class bar extends foo{
}

and I'd like to be able to grab an array like:

array('boo','bar');

 Answers

51

If you need that, it really smells like bad code, the base class shouldn't need to know this.

However, if you definitions have been included (i.e. you don't need to include new files with classes you possibly have), you could run:

$children  = array();
foreach(get_declared_classes() as $class){
    if($class instanceof foo) $children[] = $class;
}
Wednesday, March 31, 2021
 
relipse
answered 7 Months ago
55

Classes are a notion of object-oriented design (and programming and analysis, respectively), where they are used to encapsulate data and methods.

Other object-oriented programming techniques may include features such as

  • information hiding,
  • data abstraction,
  • encapsulation,
  • modularity,
  • polymorphism and
  • inheritance

From an article .. top-15-best-practices-for-writing-super-readable-code:

Object oriented programming can help you create well structured code. But that does not mean you need to abandon procedural programming completely. Actually creating a mix of both styles can be good.

From http://java.sun.com/docs/books/tutorial/java/concepts/class.html:

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

Finally, a short youtube video about the differences between the procedural and object-oriented programming paradigm ...

Wednesday, March 31, 2021
 
OMGKurtNilsen
answered 7 Months ago
67

A base class should really never depend on information about child classes---

To answer your question:

class base {
    public function __construct() {
        print "Class:" . get_class($this) . "n";
    }
}

class child extends base{
    public function __construct() {
        parent::__construct();
    }
}
$c = new child();

Just for future reference -- this can be acheived in a static context using get_called_class(), but this is only available in PHP >= 5.3

Wednesday, March 31, 2021
 
Strae
answered 7 Months ago
64

You have to make an AJAX call:

var lis = new Array();

// Iterate through the <li> items
$("#tag-cloud-list").children("li").each(function()
{
    lis.push($(this).text());
});

// Make AJAX call and set data to something like "Music::Lion::Dwarf"
$.ajax({
    url: "dostuff.php",
    type: "POST",
    data: { items: lis.join("::") },
    success: function() { alert("OK"); }
});

Then in the PHP script, use

$lis = $_POST['items'];
$liarray = explode("::", $lis);

to retrieve an array of the li items

Saturday, May 29, 2021
 
Bono
answered 5 Months ago
59

Yes, it is possible, that is know as variable functions, have a look at this.

Example from PHP's official site:

<?php
class Foo
{
    function Variable()
    {
        $name = 'Bar';
        $this->$name(); // This calls the Bar() method
    }

    function Bar()
    {
        echo "This is Bar";
    }
}

$foo = new Foo();
$funcname = "Variable";
$foo->$funcname();  // This calls $foo->Variable()

?>

In your case, make sure that the function do_the_thing exists. Also note that you are storing the return value of the function:

$req = $class->$function_name();

Try to see what the variable $req contains. For example this should give you info:

print_r($req); // or simple echo as per return value of your function

Note:

Variable functions won't work with language constructs such as echo(), print(), unset(), isset(), empty(), include(), require() and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.

Friday, September 24, 2021
 
Good Person
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share