Jump to content

Extending class


matjazp
 Share

Recommended Posts

<?php 
// in mytemplate.php

echo "Roles:" . wireClassExists('Roles') . "<br>"; // expecting true
echo "foo:" . wireClassExists('foo') ."<br>"; // expecting false

class bar extends Roles { }
echo "bar: " . wireClassExists('bar') ."<br>"; //expecting true

Output:

Roles:1
foo:
bar: 

If I add namespace ProcessWire in mytemplate.php, then the result is as expected, class bar exists:

<?php namespace ProcessWire;
// in mytemplate.php

echo "Roles:" . wireClassExists('Roles') . "<br>"; // expecting true
echo "foo:" . wireClassExists('foo') ."<br>"; // expecting false

class bar extends Roles { }
echo "bar: " . wireClassExists('bar') ."<br>"; //expecting true

Output:

Roles:1
foo:
bar: 1

This is how it should be?

  • Like 1
Link to comment
Share on other sites

The Function wireClassExists() is described as follows:

ProcessWire namespace aware version of PHP's class_exists() function

 

The function returns the expected value, without a defined namespace
<?php
class bar extends Roles { }

// namespace aware function
var_dump(wireClassExists('bar')); // return false
var_dump(wireClassExists('\bar')); // return true

// not namespace aware function
var_dump(class_exists('bar')); // return true


and with defined namespace

<?php namespace ProcessWire;
class bar extends Roles { }

// namespace aware function
var_dump(wireClassExists('bar')); // return true
var_dump(wireClassExists('\bar')); // return false

// not namespace aware function
var_dump(class_exists('bar')); // return false

 

  • Like 6
Link to comment
Share on other sites

Thx, kixe. I wrongly expected that wireClassExists('bar') would always return true, with or without namespace. Because this is not the case, this is why DynamicRoles module (and possible others?) doesn't work on PW3 unless you add namespace.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...