PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms 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
 1<?php
 2
 3# This file illustrates the cross language polymorphism using directors.
 4
 5require("example.php");
 6
 7# CEO class, which overrides Employee::getPosition().
 8
 9class 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");
20print $e->getName() . " is a " . $e->getPosition() . "\n";
21printf("Just call her \"%s\"\n", $e->getTitle());
22print "----------------------\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);
35print "----------------------\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
54print "(position, title) for items 0-3:\n";
55
56printf("  %s, \"%s\"\n", $list->get_item(0)->getPosition(), $list->get_item(0)->getTitle());
57printf("  %s, \"%s\"\n", $list->get_item(1)->getPosition(), $list->get_item(1)->getTitle());
58printf("  %s, \"%s\"\n", $list->get_item(2)->getPosition(), $list->get_item(2)->getTitle());
59printf("  %s, \"%s\"\n", $list->get_item(3)->getPosition(), $list->get_item(3)->getTitle());
60print "----------------------\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
69unset($list);
70print "----------------------\n";
71
72# All done.
73
74print "php exit\n";
75
76?>