• I’ve run into another anomaly with my Domain objects snippet (I finally had to give up on putting these in a namespace. They were constantly “not found.”)

    I have this snippet:

    /**

    • SB Model API
      */
      if (!defined(‘ABSPATH’)) exit;

    /**

    • ————————-
    • Consultant Class
    • */
      class Consultant {
      public string $id;
      public string $name;
      public string $email;
      public string $calendly_link;
      public string $tag;
      public string $tagId;
      public ?\DateTime $created_at;
      public ?\DateTime $updated_at; protected static array $cache = []; public function __construct(
      string $id,
      string $name,
      string $email,
      string $calendly_link,
      string $tag = ”,
      string $tagId = ”,
      ?\DateTime $created_at = null,
      ?\DateTime $updated_at = null
      ) {
      $this->id = $id;
      $this->name = $name;
      $this->email = $email;
      $this->calendly_link = $calendly_link;
      $this->tag = $tag;
      $this->tagId = $tagId;
      $this->created_at = $created_at;
      $this->updated_at = $updated_at;
      } public static function find(?string $id = null): ?self {
      $id = $id ?: ‘sb’;
      if (isset(self::$cache[$id])) return self::$cache[$id]; global $wpdb; $table = $wpdb->prefix . 'sba_consultants'; $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE public_id = %s", $id), ARRAY_A); if (!$row) return null; $consultant = new self( $row['public_id'], $row['name'], $row['email'], $row['calendly_link'] ?? '', $row['tag'] ?? '', $row['tagId'] ?? '', new \DateTime($row['created_at']), new \DateTime($row['updated_at']) ); self::$cache[$id] = $consultant; return $consultant; } public static function find_all(): array {
      global $wpdb;
      $table = $wpdb->prefix . ‘sba_consultants’;
      $rows = $wpdb->get_results(“SELECT * FROM {$table}”, ARRAY_A); $result = []; foreach ($rows as $row) { $consultant = new self( $row['public_id'], $row['name'], $row['email'], $row['calendly_link'] ?? '', $row['tag'] ?? '', $row['tagId'] ?? '', new \DateTime($row['created_at']), new \DateTime($row['updated_at']) ); self::$cache[$row['public_id']] = $consultant; $result[] = $consultant; } return $result; }
      }

    /**

    • ————————-
    • Client Class
    • */
      class Client {
      public string $email;
      public ?string $name;
      public string $consultant_public_id;
      public ?string $attribution_log; // <– JSON log for attribution events
      public ?\DateTime $created_at;
      public ?\DateTime $updated_at; protected static array $cache = []; public function __construct(
      string $email,
      ?string $name,
      string $consultant_public_id,
      ?string $attribution_log = null,
      ?\DateTime $created_at = null,
      ?\DateTime $updated_at = null
      ) {
      $this->email = $email;
      $this->name = $name;
      $this->consultant_public_id = $consultant_public_id;
      $this->attribution_log = $attribution_log;
      $this->created_at = $created_at;
      $this->updated_at = $updated_at;
      } // ——————————-
      // Find client by email
      // ——————————-
      public static function find_by_email(string $email): ?self {
      if (isset(self::$cache[$email])) return self::$cache[$email]; global $wpdb; $table = $wpdb->prefix . 'sba_clients'; $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE email = %s", $email), ARRAY_A); if (!$row) return null; $client = new self( $row['email'], $row['name'] ?? null, $row['consultant_public_id'], $row['attribution_log'] ?? null, new \DateTime($row['created_at']), new \DateTime($row['updated_at']) ); self::$cache[$email] = $client; return $client; } // ——————————-
      // Find all clients by consultant
      // ——————————-
      public static function find_all_by_consultant(string $consultant_public_id): array {
      global $wpdb;
      $table = $wpdb->prefix . ‘sba_clients’;
      $rows = $wpdb->get_results($wpdb->prepare(“SELECT * FROM {$table} WHERE consultant_public_id = %s”, $consultant_public_id), ARRAY_A); $result = []; foreach ($rows as $row) { $client = new self( $row['email'], $row['name'] ?? null, $row['consultant_public_id'], $row['attribution_log'] ?? null, new \DateTime($row['created_at']), new \DateTime($row['updated_at']) ); self::$cache[$row['email']] = $client; $result[] = $client; } return $result; } // ——————————-
      // Create a new client
      // ——————————-
      public static function create(array $data): self {
      global $wpdb;
      $table = $wpdb->prefix . ‘sba_clients’; $wpdb->insert($table, [ 'email' => $data['email'], 'name' => $data['name'] ?? null, 'consultant_public_id' => $data['consultant_public_id'], 'attribution_log' => $data['attribution_log'] ?? null, 'created_at' => current_time('mysql'), 'updated_at' => current_time('mysql') ]); return self::find_by_email($data['email']); } // ——————————-
      // Save changes to an existing client
      // ——————————-
      public function save(): void {
      global $wpdb;
      $table = $wpdb->prefix . ‘sba_clients’; $wpdb->update( $table, [ 'name' => $this->name, 'consultant_public_id' => $this->consultant_public_id, 'attribution_log' => $this->attribution_log, 'updated_at' => current_time('mysql'), ], ['email' => $this->email] ); // Update cache self::$cache[$this->email] = $this; }
      }

    As long as they both have create and/or save functions, the snippet won’t activate. If I take those functions out of either class then it WILL activate.

    Why won’t the snippet activate with both classes having the functions they obviously NEED?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter csdsubscriber

    (@csdsubscriber)

    Just to add to this, I attempted to move the Client class into a second snippet. The first snippet with the Consultant class activated fine, but the second snippet was still a no go.

    So there appears to be some very unobvious conflict happening. Hope there is an answer as I really NEED to get these classes working asap.

    Thread Starter csdsubscriber

    (@csdsubscriber)

    Okay I think I’ve figured it out “sort of.” It must be because they are all static functions, that they are ending up in the same “space?” I changed the named to create_consultant, save_consultant, create_client and save_client and the snippet activated.

    Would be great to understand though why this is the case, since those static functions are still inside different class definitions.

    Thread Starter csdsubscriber

    (@csdsubscriber)

    Since same named static functions INSIDE different classes is valid PHP, safe to assume this is a Code Snippets validator BUG?

    Plugin Author Carolina

    (@carolinaop)

    Hello @csdsubscriber,

    Thanks for the detailed report and for sharing your code.

    Static methods with the same name in different classes are valid PHP and are supported in Code Snippets. So the activation failure is almost certainly caused by something else: for example, a duplicate global function/class defined by another snippet/theme/plugin, or a subtle syntax/structure issue that makes part of your code look like a global function instead of a class method. Please make sure the snippet uses regular ASCII quotes and clean formatting.

    Your current workaround (e.g. create_client, save_client) is safe to keep using, but the underlying problem is not that Code Snippets forbids same‑named static methods in different classes.

    Let us know if this helps 🙂

    Thread Starter csdsubscriber

    (@csdsubscriber)

    That doesn’t add up though. If it was a clash with something else globally then even one such function should have caused it not to activate. It activated fine when only one such public static function was present. It was only when the same signature was added in a second class that activation would not happen.

    I’ve ended up going with a true base class now that implements the public static functions. For my purposes this is an adequate work around, but it would still be nice to get to the bottom of why activation would not happen.

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.