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