PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/php/extend/runme.php

#
PHP | 76 lines | 25 code | 18 blank | 33 comment | 1 complexity | 83980aeacac6039cb16571b0d21a7257 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <?php
  2. # This file illustrates the cross language polymorphism using directors.
  3. require("example.php");
  4. # CEO class, which overrides Employee::getPosition().
  5. class CEO extends Manager {
  6. function getPosition() {
  7. return "CEO";
  8. }
  9. }
  10. # Create an instance of our employee extension class, CEO. The calls to
  11. # getName() and getPosition() are standard, the call to getTitle() uses
  12. # the director wrappers to call CEO.getPosition.
  13. $e = new CEO("Alice");
  14. print $e->getName() . " is a " . $e->getPosition() . "\n";
  15. printf("Just call her \"%s\"\n", $e->getTitle());
  16. print "----------------------\n";
  17. # Create a new EmployeeList instance. This class does not have a C++
  18. # director wrapper, but can be used freely with other classes that do.
  19. $list = new EmployeeList();
  20. # EmployeeList owns its items, so we must surrender ownership of objects
  21. # we add. This involves first clearing the ->disown member to tell the
  22. # C++ director to start reference counting.
  23. $e->thisown = 0;
  24. $list->addEmployee($e);
  25. print "----------------------\n";
  26. # Now we access the first four items in list (three are C++ objects that
  27. # EmployeeList's constructor adds, the last is our CEO). The virtual
  28. # methods of all these instances are treated the same. For items 0, 1, and
  29. # 2, both all methods resolve in C++. For item 3, our CEO, getTitle calls
  30. # getPosition which resolves in PHP. The call to getPosition is
  31. # slightly different, however, from the e.getPosition() call above, since
  32. # now the object reference has been "laundered" by passing through
  33. # EmployeeList as an Employee*. Previously, PHP resolved the call
  34. # immediately in CEO, but now PHP thinks the object is an instance of
  35. # class Employee (actually EmployeePtr). So the call passes through the
  36. # Employee proxy class and on to the C wrappers and C++ director,
  37. # eventually ending up back at the CEO implementation of getPosition().
  38. # The call to getTitle() for item 3 runs the C++ Employee::getTitle()
  39. # method, which in turn calls getPosition(). This virtual method call
  40. # passes down through the C++ director class to the PHP implementation
  41. # in CEO. All this routing takes place transparently.
  42. print "(position, title) for items 0-3:\n";
  43. printf(" %s, \"%s\"\n", $list->get_item(0)->getPosition(), $list->get_item(0)->getTitle());
  44. printf(" %s, \"%s\"\n", $list->get_item(1)->getPosition(), $list->get_item(1)->getTitle());
  45. printf(" %s, \"%s\"\n", $list->get_item(2)->getPosition(), $list->get_item(2)->getTitle());
  46. printf(" %s, \"%s\"\n", $list->get_item(3)->getPosition(), $list->get_item(3)->getTitle());
  47. print "----------------------\n";
  48. # Time to delete the EmployeeList, which will delete all the Employee*
  49. # items it contains. The last item is our CEO, which gets destroyed as its
  50. # reference count goes to zero. The PHP destructor runs, and is still
  51. # able to call the getName() method since the underlying C++ object still
  52. # exists. After this destructor runs the remaining C++ destructors run as
  53. # usual to destroy the object.
  54. unset($list);
  55. print "----------------------\n";
  56. # All done.
  57. print "php exit\n";
  58. ?>