Component.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. use Yii;
  9. /**
  10. * Component is the base class that implements the *property*, *event* and *behavior* features.
  11. *
  12. * Component provides the *event* and *behavior* features, in addition to the *property* feature which is implemented in
  13. * its parent class [[\yii\base\Object|Object]].
  14. *
  15. * Event is a way to "inject" custom code into existing code at certain places. For example, a comment object can trigger
  16. * an "add" event when the user adds a comment. We can write custom code and attach it to this event so that when the event
  17. * is triggered (i.e. comment will be added), our custom code will be executed.
  18. *
  19. * An event is identified by a name that should be unique within the class it is defined at. Event names are *case-sensitive*.
  20. *
  21. * One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[trigger()]] to
  22. * raise an event. When an event is raised, the event handlers will be invoked automatically in the order they were
  23. * attached.
  24. *
  25. * To attach an event handler to an event, call [[on()]]:
  26. *
  27. * ```php
  28. * $post->on('update', function ($event) {
  29. * // send email notification
  30. * });
  31. * ```
  32. *
  33. * In the above, an anonymous function is attached to the "update" event of the post. You may attach
  34. * the following types of event handlers:
  35. *
  36. * - anonymous function: `function ($event) { ... }`
  37. * - object method: `[$object, 'handleAdd']`
  38. * - static class method: `['Page', 'handleAdd']`
  39. * - global function: `'handleAdd'`
  40. *
  41. * The signature of an event handler should be like the following:
  42. *
  43. * ```php
  44. * function foo($event)
  45. * ```
  46. *
  47. * where `$event` is an [[Event]] object which includes parameters associated with the event.
  48. *
  49. * You can also attach a handler to an event when configuring a component with a configuration array.
  50. * The syntax is like the following:
  51. *
  52. * ```php
  53. * [
  54. * 'on add' => function ($event) { ... }
  55. * ]
  56. * ```
  57. *
  58. * where `on add` stands for attaching an event to the `add` event.
  59. *
  60. * Sometimes, you may want to associate extra data with an event handler when you attach it to an event
  61. * and then access it when the handler is invoked. You may do so by
  62. *
  63. * ```php
  64. * $post->on('update', function ($event) {
  65. * // the data can be accessed via $event->data
  66. * }, $data);
  67. * ```
  68. *
  69. * A behavior is an instance of [[Behavior]] or its child class. A component can be attached with one or multiple
  70. * behaviors. When a behavior is attached to a component, its public properties and methods can be accessed via the
  71. * component directly, as if the component owns those properties and methods.
  72. *
  73. * To attach a behavior to a component, declare it in [[behaviors()]], or explicitly call [[attachBehavior]]. Behaviors
  74. * declared in [[behaviors()]] are automatically attached to the corresponding component.
  75. *
  76. * One can also attach a behavior to a component when configuring it with a configuration array. The syntax is like the
  77. * following:
  78. *
  79. * ```php
  80. * [
  81. * 'as tree' => [
  82. * 'class' => 'Tree',
  83. * ],
  84. * ]
  85. * ```
  86. *
  87. * where `as tree` stands for attaching a behavior named `tree`, and the array will be passed to [[\Yii::createObject()]]
  88. * to create the behavior object.
  89. *
  90. * For more details and usage information on Component, see the [guide article on components](guide:concept-components).
  91. *
  92. * @property Behavior[] $behaviors List of behaviors attached to this component. This property is read-only.
  93. *
  94. * @author Qiang Xue <qiang.xue@gmail.com>
  95. * @since 2.0
  96. */
  97. class Component extends Object
  98. {
  99. /**
  100. * @var array the attached event handlers (event name => handlers)
  101. */
  102. private $_events = [];
  103. /**
  104. * @var Behavior[]|null the attached behaviors (behavior name => behavior). This is `null` when not initialized.
  105. */
  106. private $_behaviors;
  107. /**
  108. * Returns the value of a component property.
  109. * This method will check in the following order and act accordingly:
  110. *
  111. * - a property defined by a getter: return the getter result
  112. * - a property of a behavior: return the behavior property value
  113. *
  114. * Do not call this method directly as it is a PHP magic method that
  115. * will be implicitly called when executing `$value = $component->property;`.
  116. * @param string $name the property name
  117. * @return mixed the property value or the value of a behavior's property
  118. * @throws UnknownPropertyException if the property is not defined
  119. * @throws InvalidCallException if the property is write-only.
  120. * @see __set()
  121. */
  122. public function __get($name)
  123. {
  124. $getter = 'get' . $name;
  125. if (method_exists($this, $getter)) {
  126. // read property, e.g. getName()
  127. return $this->$getter();
  128. }
  129. // behavior property
  130. $this->ensureBehaviors();
  131. foreach ($this->_behaviors as $behavior) {
  132. if ($behavior->canGetProperty($name)) {
  133. return $behavior->$name;
  134. }
  135. }
  136. if (method_exists($this, 'set' . $name)) {
  137. throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
  138. }
  139. throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
  140. }
  141. /**
  142. * Sets the value of a component property.
  143. * This method will check in the following order and act accordingly:
  144. *
  145. * - a property defined by a setter: set the property value
  146. * - an event in the format of "on xyz": attach the handler to the event "xyz"
  147. * - a behavior in the format of "as xyz": attach the behavior named as "xyz"
  148. * - a property of a behavior: set the behavior property value
  149. *
  150. * Do not call this method directly as it is a PHP magic method that
  151. * will be implicitly called when executing `$component->property = $value;`.
  152. * @param string $name the property name or the event name
  153. * @param mixed $value the property value
  154. * @throws UnknownPropertyException if the property is not defined
  155. * @throws InvalidCallException if the property is read-only.
  156. * @see __get()
  157. */
  158. public function __set($name, $value)
  159. {
  160. $setter = 'set' . $name;
  161. if (method_exists($this, $setter)) {
  162. // set property
  163. $this->$setter($value);
  164. return;
  165. } elseif (strncmp($name, 'on ', 3) === 0) {
  166. // on event: attach event handler
  167. $this->on(trim(substr($name, 3)), $value);
  168. return;
  169. } elseif (strncmp($name, 'as ', 3) === 0) {
  170. // as behavior: attach behavior
  171. $name = trim(substr($name, 3));
  172. $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
  173. return;
  174. }
  175. // behavior property
  176. $this->ensureBehaviors();
  177. foreach ($this->_behaviors as $behavior) {
  178. if ($behavior->canSetProperty($name)) {
  179. $behavior->$name = $value;
  180. return;
  181. }
  182. }
  183. if (method_exists($this, 'get' . $name)) {
  184. throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
  185. }
  186. throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
  187. }
  188. /**
  189. * Checks if a property is set, i.e. defined and not null.
  190. * This method will check in the following order and act accordingly:
  191. *
  192. * - a property defined by a setter: return whether the property is set
  193. * - a property of a behavior: return whether the property is set
  194. * - return `false` for non existing properties
  195. *
  196. * Do not call this method directly as it is a PHP magic method that
  197. * will be implicitly called when executing `isset($component->property)`.
  198. * @param string $name the property name or the event name
  199. * @return bool whether the named property is set
  200. * @see http://php.net/manual/en/function.isset.php
  201. */
  202. public function __isset($name)
  203. {
  204. $getter = 'get' . $name;
  205. if (method_exists($this, $getter)) {
  206. return $this->$getter() !== null;
  207. }
  208. // behavior property
  209. $this->ensureBehaviors();
  210. foreach ($this->_behaviors as $behavior) {
  211. if ($behavior->canGetProperty($name)) {
  212. return $behavior->$name !== null;
  213. }
  214. }
  215. return false;
  216. }
  217. /**
  218. * Sets a component property to be null.
  219. * This method will check in the following order and act accordingly:
  220. *
  221. * - a property defined by a setter: set the property value to be null
  222. * - a property of a behavior: set the property value to be null
  223. *
  224. * Do not call this method directly as it is a PHP magic method that
  225. * will be implicitly called when executing `unset($component->property)`.
  226. * @param string $name the property name
  227. * @throws InvalidCallException if the property is read only.
  228. * @see http://php.net/manual/en/function.unset.php
  229. */
  230. public function __unset($name)
  231. {
  232. $setter = 'set' . $name;
  233. if (method_exists($this, $setter)) {
  234. $this->$setter(null);
  235. return;
  236. }
  237. // behavior property
  238. $this->ensureBehaviors();
  239. foreach ($this->_behaviors as $behavior) {
  240. if ($behavior->canSetProperty($name)) {
  241. $behavior->$name = null;
  242. return;
  243. }
  244. }
  245. throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
  246. }
  247. /**
  248. * Calls the named method which is not a class method.
  249. *
  250. * This method will check if any attached behavior has
  251. * the named method and will execute it if available.
  252. *
  253. * Do not call this method directly as it is a PHP magic method that
  254. * will be implicitly called when an unknown method is being invoked.
  255. * @param string $name the method name
  256. * @param array $params method parameters
  257. * @return mixed the method return value
  258. * @throws UnknownMethodException when calling unknown method
  259. */
  260. public function __call($name, $params)
  261. {
  262. $this->ensureBehaviors();
  263. foreach ($this->_behaviors as $object) {
  264. if ($object->hasMethod($name)) {
  265. return call_user_func_array([$object, $name], $params);
  266. }
  267. }
  268. throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
  269. }
  270. /**
  271. * This method is called after the object is created by cloning an existing one.
  272. * It removes all behaviors because they are attached to the old object.
  273. */
  274. public function __clone()
  275. {
  276. $this->_events = [];
  277. $this->_behaviors = null;
  278. }
  279. /**
  280. * Returns a value indicating whether a property is defined for this component.
  281. * A property is defined if:
  282. *
  283. * - the class has a getter or setter method associated with the specified name
  284. * (in this case, property name is case-insensitive);
  285. * - the class has a member variable with the specified name (when `$checkVars` is true);
  286. * - an attached behavior has a property of the given name (when `$checkBehaviors` is true).
  287. *
  288. * @param string $name the property name
  289. * @param bool $checkVars whether to treat member variables as properties
  290. * @param bool $checkBehaviors whether to treat behaviors' properties as properties of this component
  291. * @return bool whether the property is defined
  292. * @see canGetProperty()
  293. * @see canSetProperty()
  294. */
  295. public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
  296. {
  297. return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
  298. }
  299. /**
  300. * Returns a value indicating whether a property can be read.
  301. * A property can be read if:
  302. *
  303. * - the class has a getter method associated with the specified name
  304. * (in this case, property name is case-insensitive);
  305. * - the class has a member variable with the specified name (when `$checkVars` is true);
  306. * - an attached behavior has a readable property of the given name (when `$checkBehaviors` is true).
  307. *
  308. * @param string $name the property name
  309. * @param bool $checkVars whether to treat member variables as properties
  310. * @param bool $checkBehaviors whether to treat behaviors' properties as properties of this component
  311. * @return bool whether the property can be read
  312. * @see canSetProperty()
  313. */
  314. public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
  315. {
  316. if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
  317. return true;
  318. } elseif ($checkBehaviors) {
  319. $this->ensureBehaviors();
  320. foreach ($this->_behaviors as $behavior) {
  321. if ($behavior->canGetProperty($name, $checkVars)) {
  322. return true;
  323. }
  324. }
  325. }
  326. return false;
  327. }
  328. /**
  329. * Returns a value indicating whether a property can be set.
  330. * A property can be written if:
  331. *
  332. * - the class has a setter method associated with the specified name
  333. * (in this case, property name is case-insensitive);
  334. * - the class has a member variable with the specified name (when `$checkVars` is true);
  335. * - an attached behavior has a writable property of the given name (when `$checkBehaviors` is true).
  336. *
  337. * @param string $name the property name
  338. * @param bool $checkVars whether to treat member variables as properties
  339. * @param bool $checkBehaviors whether to treat behaviors' properties as properties of this component
  340. * @return bool whether the property can be written
  341. * @see canGetProperty()
  342. */
  343. public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
  344. {
  345. if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
  346. return true;
  347. } elseif ($checkBehaviors) {
  348. $this->ensureBehaviors();
  349. foreach ($this->_behaviors as $behavior) {
  350. if ($behavior->canSetProperty($name, $checkVars)) {
  351. return true;
  352. }
  353. }
  354. }
  355. return false;
  356. }
  357. /**
  358. * Returns a value indicating whether a method is defined.
  359. * A method is defined if:
  360. *
  361. * - the class has a method with the specified name
  362. * - an attached behavior has a method with the given name (when `$checkBehaviors` is true).
  363. *
  364. * @param string $name the property name
  365. * @param bool $checkBehaviors whether to treat behaviors' methods as methods of this component
  366. * @return bool whether the method is defined
  367. */
  368. public function hasMethod($name, $checkBehaviors = true)
  369. {
  370. if (method_exists($this, $name)) {
  371. return true;
  372. } elseif ($checkBehaviors) {
  373. $this->ensureBehaviors();
  374. foreach ($this->_behaviors as $behavior) {
  375. if ($behavior->hasMethod($name)) {
  376. return true;
  377. }
  378. }
  379. }
  380. return false;
  381. }
  382. /**
  383. * Returns a list of behaviors that this component should behave as.
  384. *
  385. * Child classes may override this method to specify the behaviors they want to behave as.
  386. *
  387. * The return value of this method should be an array of behavior objects or configurations
  388. * indexed by behavior names. A behavior configuration can be either a string specifying
  389. * the behavior class or an array of the following structure:
  390. *
  391. * ```php
  392. * 'behaviorName' => [
  393. * 'class' => 'BehaviorClass',
  394. * 'property1' => 'value1',
  395. * 'property2' => 'value2',
  396. * ]
  397. * ```
  398. *
  399. * Note that a behavior class must extend from [[Behavior]]. Behaviors can be attached using a name or anonymously.
  400. * When a name is used as the array key, using this name, the behavior can later be retrieved using [[getBehavior()]]
  401. * or be detached using [[detachBehavior()]]. Anonymous behaviors can not be retrieved or detached.
  402. *
  403. * Behaviors declared in this method will be attached to the component automatically (on demand).
  404. *
  405. * @return array the behavior configurations.
  406. */
  407. public function behaviors()
  408. {
  409. return [];
  410. }
  411. /**
  412. * Returns a value indicating whether there is any handler attached to the named event.
  413. * @param string $name the event name
  414. * @return bool whether there is any handler attached to the event.
  415. */
  416. public function hasEventHandlers($name)
  417. {
  418. $this->ensureBehaviors();
  419. return !empty($this->_events[$name]) || Event::hasHandlers($this, $name);
  420. }
  421. /**
  422. * Attaches an event handler to an event.
  423. *
  424. * The event handler must be a valid PHP callback. The following are
  425. * some examples:
  426. *
  427. * ```
  428. * function ($event) { ... } // anonymous function
  429. * [$object, 'handleClick'] // $object->handleClick()
  430. * ['Page', 'handleClick'] // Page::handleClick()
  431. * 'handleClick' // global function handleClick()
  432. * ```
  433. *
  434. * The event handler must be defined with the following signature,
  435. *
  436. * ```
  437. * function ($event)
  438. * ```
  439. *
  440. * where `$event` is an [[Event]] object which includes parameters associated with the event.
  441. *
  442. * @param string $name the event name
  443. * @param callable $handler the event handler
  444. * @param mixed $data the data to be passed to the event handler when the event is triggered.
  445. * When the event handler is invoked, this data can be accessed via [[Event::data]].
  446. * @param bool $append whether to append new event handler to the end of the existing
  447. * handler list. If false, the new handler will be inserted at the beginning of the existing
  448. * handler list.
  449. * @see off()
  450. */
  451. public function on($name, $handler, $data = null, $append = true)
  452. {
  453. $this->ensureBehaviors();
  454. if ($append || empty($this->_events[$name])) {
  455. $this->_events[$name][] = [$handler, $data];
  456. } else {
  457. array_unshift($this->_events[$name], [$handler, $data]);
  458. }
  459. }
  460. /**
  461. * Detaches an existing event handler from this component.
  462. * This method is the opposite of [[on()]].
  463. * @param string $name event name
  464. * @param callable $handler the event handler to be removed.
  465. * If it is null, all handlers attached to the named event will be removed.
  466. * @return bool if a handler is found and detached
  467. * @see on()
  468. */
  469. public function off($name, $handler = null)
  470. {
  471. $this->ensureBehaviors();
  472. if (empty($this->_events[$name])) {
  473. return false;
  474. }
  475. if ($handler === null) {
  476. unset($this->_events[$name]);
  477. return true;
  478. }
  479. $removed = false;
  480. foreach ($this->_events[$name] as $i => $event) {
  481. if ($event[0] === $handler) {
  482. unset($this->_events[$name][$i]);
  483. $removed = true;
  484. }
  485. }
  486. if ($removed) {
  487. $this->_events[$name] = array_values($this->_events[$name]);
  488. }
  489. return $removed;
  490. }
  491. /**
  492. * Triggers an event.
  493. * This method represents the happening of an event. It invokes
  494. * all attached handlers for the event including class-level handlers.
  495. * @param string $name the event name
  496. * @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
  497. */
  498. public function trigger($name, Event $event = null)
  499. {
  500. $this->ensureBehaviors();
  501. if (!empty($this->_events[$name])) {
  502. if ($event === null) {
  503. $event = new Event;
  504. }
  505. if ($event->sender === null) {
  506. $event->sender = $this;
  507. }
  508. $event->handled = false;
  509. $event->name = $name;
  510. foreach ($this->_events[$name] as $handler) {
  511. $event->data = $handler[1];
  512. call_user_func($handler[0], $event);
  513. // stop further handling if the event is handled
  514. if ($event->handled) {
  515. return;
  516. }
  517. }
  518. }
  519. // invoke class-level attached handlers
  520. Event::trigger($this, $name, $event);
  521. }
  522. /**
  523. * Returns the named behavior object.
  524. * @param string $name the behavior name
  525. * @return null|Behavior the behavior object, or null if the behavior does not exist
  526. */
  527. public function getBehavior($name)
  528. {
  529. $this->ensureBehaviors();
  530. return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
  531. }
  532. /**
  533. * Returns all behaviors attached to this component.
  534. * @return Behavior[] list of behaviors attached to this component
  535. */
  536. public function getBehaviors()
  537. {
  538. $this->ensureBehaviors();
  539. return $this->_behaviors;
  540. }
  541. /**
  542. * Attaches a behavior to this component.
  543. * This method will create the behavior object based on the given
  544. * configuration. After that, the behavior object will be attached to
  545. * this component by calling the [[Behavior::attach()]] method.
  546. * @param string $name the name of the behavior.
  547. * @param string|array|Behavior $behavior the behavior configuration. This can be one of the following:
  548. *
  549. * - a [[Behavior]] object
  550. * - a string specifying the behavior class
  551. * - an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object.
  552. *
  553. * @return Behavior the behavior object
  554. * @see detachBehavior()
  555. */
  556. public function attachBehavior($name, $behavior)
  557. {
  558. $this->ensureBehaviors();
  559. return $this->attachBehaviorInternal($name, $behavior);
  560. }
  561. /**
  562. * Attaches a list of behaviors to the component.
  563. * Each behavior is indexed by its name and should be a [[Behavior]] object,
  564. * a string specifying the behavior class, or an configuration array for creating the behavior.
  565. * @param array $behaviors list of behaviors to be attached to the component
  566. * @see attachBehavior()
  567. */
  568. public function attachBehaviors($behaviors)
  569. {
  570. $this->ensureBehaviors();
  571. foreach ($behaviors as $name => $behavior) {
  572. $this->attachBehaviorInternal($name, $behavior);
  573. }
  574. }
  575. /**
  576. * Detaches a behavior from the component.
  577. * The behavior's [[Behavior::detach()]] method will be invoked.
  578. * @param string $name the behavior's name.
  579. * @return null|Behavior the detached behavior. Null if the behavior does not exist.
  580. */
  581. public function detachBehavior($name)
  582. {
  583. $this->ensureBehaviors();
  584. if (isset($this->_behaviors[$name])) {
  585. $behavior = $this->_behaviors[$name];
  586. unset($this->_behaviors[$name]);
  587. $behavior->detach();
  588. return $behavior;
  589. }
  590. return null;
  591. }
  592. /**
  593. * Detaches all behaviors from the component.
  594. */
  595. public function detachBehaviors()
  596. {
  597. $this->ensureBehaviors();
  598. foreach ($this->_behaviors as $name => $behavior) {
  599. $this->detachBehavior($name);
  600. }
  601. }
  602. /**
  603. * Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
  604. */
  605. public function ensureBehaviors()
  606. {
  607. if ($this->_behaviors === null) {
  608. $this->_behaviors = [];
  609. foreach ($this->behaviors() as $name => $behavior) {
  610. $this->attachBehaviorInternal($name, $behavior);
  611. }
  612. }
  613. }
  614. /**
  615. * Attaches a behavior to this component.
  616. * @param string|int $name the name of the behavior. If this is an integer, it means the behavior
  617. * is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name
  618. * will be detached first.
  619. * @param string|array|Behavior $behavior the behavior to be attached
  620. * @return Behavior the attached behavior.
  621. */
  622. private function attachBehaviorInternal($name, $behavior)
  623. {
  624. if (!($behavior instanceof Behavior)) {
  625. $behavior = Yii::createObject($behavior);
  626. }
  627. if (is_int($name)) {
  628. $behavior->attach($this);
  629. $this->_behaviors[] = $behavior;
  630. }
  631. if (isset($this->_behaviors[$name])) {
  632. $this->_behaviors[$name]->detach();
  633. }
  634. $behavior->attach($this);
  635. $this->_behaviors[$name] = $behavior;
  636. return $behavior;
  637. }
  638. }